CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Process Management / fg

fg

Bring a job back to the foreground

Beginnerjob controlshell

fg brings a background or stopped job back to the foreground: it gets the terminal again, receives your keystrokes and Ctrl-C, and the shell waits for it. It's how you return to a vim you suspended with Ctrl-Z or check on a job you started with &.

Syntaxfg [%JOB]

Watch it run

While editing a config you need to check a value on the box, then get back to exactly where you were.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
vim /etc/nginx/sites-available/app

In vim, Ctrl-Z suspends the editor. Unsaved changes, cursor position and undo history all stay in the stopped process.

Anatomy of a command

$
commandfg

bash builtin: gives the terminal to the job and resumes it.

How it works

fg finds the job in bash's job table, prints its command line, makes its process group the terminal's foreground group with tcsetpgrp, and sends SIGCONT to the group in case it was stopped. bash then blocks in waitpid until the job exits or stops again. From now on Ctrl-C (SIGINT), Ctrl-\ (SIGQUIT) and Ctrl-Z (SIGTSTP) go to the job, and it can read the keyboard. Full-screen programs like vim or less redraw their screen when they get SIGCONT. When the job finishes, bash takes the terminal back and prints a prompt. fg only works in a shell with job control (interactive bash, or a script with set -m); elsewhere it fails with 'no job control'.

Options you'll actually use

(no argument)The current job (+)
%NJob number N
%-The previous job (the one marked -)
%nameJob whose command starts with name
%?textJob whose command contains text
%N (alone)Typing just %1 at the prompt is shorthand for fg %1

Common mistakes

Using fg in a script to wait for a background command

Scripts run without job control, so fg fails with 'no job control'.

cmd & pid=$!; ...; wait $pid
Typing fg in a new SSH session to get back to a job from the old one

The job belonged to the old shell, which ended (and probably sent it SIGHUP).

Use tmux or screen and reattach with tmux attach
Assuming fg restarts the job from the beginning

fg continues the same process from where it stopped; it does not run the command again.

Run the command again if you really want a fresh start

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start `sleep 120 &`, bring it to the foreground, then stop it with Ctrl-C.

  2. 2

    Open `less /etc/passwd`, suspend it, run `whoami`, then go back to less.

  3. 3

    Suspend two programs (less and sleep 600), then bring back less by name.

Related commands

bgResume a stopped job in the backgroundjobsList the shell's background and stopped jobskillSend a signal to a process
PreviousbgNextnice
Linux / Process Management / fg

fg

Bring a job back to the foreground

Beginnerjob controlshell

fg brings a background or stopped job back to the foreground: it gets the terminal again, receives your keystrokes and Ctrl-C, and the shell waits for it. It's how you return to a vim you suspended with Ctrl-Z or check on a job you started with &.

Syntaxfg [%JOB]

Watch it run

While editing a config you need to check a value on the box, then get back to exactly where you were.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 3
vim /etc/nginx/sites-available/app

In vim, Ctrl-Z suspends the editor. Unsaved changes, cursor position and undo history all stay in the stopped process.

Anatomy of a command

$
commandfg

bash builtin: gives the terminal to the job and resumes it.

How it works

fg finds the job in bash's job table, prints its command line, makes its process group the terminal's foreground group with tcsetpgrp, and sends SIGCONT to the group in case it was stopped. bash then blocks in waitpid until the job exits or stops again. From now on Ctrl-C (SIGINT), Ctrl-\ (SIGQUIT) and Ctrl-Z (SIGTSTP) go to the job, and it can read the keyboard. Full-screen programs like vim or less redraw their screen when they get SIGCONT. When the job finishes, bash takes the terminal back and prints a prompt. fg only works in a shell with job control (interactive bash, or a script with set -m); elsewhere it fails with 'no job control'.

Options you'll actually use

(no argument)The current job (+)
%NJob number N
%-The previous job (the one marked -)
%nameJob whose command starts with name
%?textJob whose command contains text
%N (alone)Typing just %1 at the prompt is shorthand for fg %1

Common mistakes

Using fg in a script to wait for a background command

Scripts run without job control, so fg fails with 'no job control'.

cmd & pid=$!; ...; wait $pid
Typing fg in a new SSH session to get back to a job from the old one

The job belonged to the old shell, which ended (and probably sent it SIGHUP).

Use tmux or screen and reattach with tmux attach
Assuming fg restarts the job from the beginning

fg continues the same process from where it stopped; it does not run the command again.

Run the command again if you really want a fresh start

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Start `sleep 120 &`, bring it to the foreground, then stop it with Ctrl-C.

  2. 2

    Open `less /etc/passwd`, suspend it, run `whoami`, then go back to less.

  3. 3

    Suspend two programs (less and sleep 600), then bring back less by name.

Related commands

bgResume a stopped job in the backgroundjobsList the shell's background and stopped jobskillSend a signal to a process
PreviousbgNextnice