Foreground and background
A shell's job control: launching with &, stopping with Ctrl+Z, jobs, fg, bg, the %n job numbers, the Running, Stopped and Done states, and surviving a terminal closing with nohup and disown.
- 25 cards
- 4 topics
- Free
The outline, topic by topic
Each topic is weighted the way the exam weighs it.
- Concepts
- Questions
- Sort and link
- Scenarios
A few cards from the deck
Job: what is it?
Show the answer
A command, or chain of commands, launched by the shell, which it tracks in its table under a number [1], [2], and so on.
This number belongs to the shell and has nothing to do with the PID: %2 designates job 2, whereas the PID is a number of the whole system.
What does bash print when you type
sleep 300 &?[1] 4120[4120] 14120[1]+ Done sleep 300
Show the answer
[1] 4120The job number comes first in brackets, then the PID of the launched process. Done only appears later, once the job has ended.
Ctrl+Z sends … to the foreground program: it is …, not terminated, and
fgorbgmakes it resume.Show the answer
Ctrl+Z sends SIGTSTP to the foreground program: it is stopped, not terminated, and
fgorbgmakes it resume.Stopped means frozen in memory: the job shows up in jobs with the Stopped state, ready to resume where it had stopped.
Background: what is it?
Show the answer
A mode where a command runs without keeping the keyboard: the shell gives control back at once. You ask for it by ending the line with &.
The shell prints the job number and the PID, for example [1] 4120. A background job that wants to read the keyboard is stopped by the system.
A backup launched over SSH with
&stops as soon as the connection drops. What is the cause and the fix?- Its output was not redirected; relaunching with > log 2>&1 & is enough to protect it
- SIGHUP goes from the shell to its jobs; relaunch with nohup, disown or inside tmux
- SSH sends SIGKILL to any background command; it must be launched in the foreground
- The backup became a zombie when the network dropped; its parent must be killed
Show the answer
SIGHUP goes from the shell to its jobs; relaunch with nohup, disown or inside tmux
The connection dropping closes the terminal, so SIGHUP goes to the shell, then to its jobs. Neither & nor a redirection protects from that signal. A multiplexer like tmux keeps the session alive without a connection.