Jobs and the background
A terminal can run one thing at a time in the foreground, and the prompt only comes back when it finishes. Job control lets you start commands in the background, pause them, and bring them back, all from one terminal.
- Start commands in the background, and pause, resume and stop them from the shell.
- Use
jobs,fg,bgand%Njob numbers, and explain how they differ from PIDs. - Keep a command running after you log out with
nohup.
Foreground and background
A job is a process (or pipeline) started from your shell. The foreground job owns the terminal: it receives your keystrokes, and the prompt waits. A background job runs without the terminal, and the prompt is free for the next command.
Add & to start a command in the background. The shell prints its job number in brackets, then its PID:
[student@servera ~]$ sleep 500 &
[1] 762
[student@servera ~]$ sleep 501 &
[2] 763
[student@servera ~]$ jobs
[1]- Running sleep 500 &
[2]+ Running sleep 501 &The + marks the "current" job (the default for fg and bg), and - the previous one. jobs -l adds the PIDs. Job numbers belong to your shell: another terminal has its own list. They are not PIDs. Refer to a job with a percent sign: %1, %2, or %% for the current one.
Moving jobs around
Step through the life of a job:
The life of a job: Step through starting, pausing and resuming a command in your terminal.
The keys and commands that drive it:
| Do this | To |
|---|---|
Ctrl+C | Interrupt (stop for good) the foreground job |
Ctrl+Z | Pause (stop) the foreground job |
bg %N | Continue a stopped job in the background |
fg %N | Bring a job to the foreground |
kill %N | Terminate a job |
[student@servera ~]$ fg %1
sleep 500
^Z
[1]+ Stopped sleep 500
[student@servera ~]$ bg %1
[1]+ sleep 500 &
[student@servera ~]$ kill %2
[2]+ Terminated sleep 501A background job still writes to the terminal if it prints anything, which can interleave with what you are typing. Redirect its output (cmd > out.txt 2>&1 &) when it is chatty.
Surviving a logout
When a terminal window closes or a connection drops, the shell receives SIGHUP ("hangup") and passes it on to its jobs, and most programs end. (Simply typing exit does not do this: the shell leaves its background jobs running.) Two ways to protect a job:
nohupstarts a command immune to hangups and sends its output tonohup.out:
[student@servera ~]$ nohup sleep 700 &
[3] 766
nohup: ignoring input and appending output to 'nohup.out'- A terminal multiplexer such as
tmuxkeeps a whole session alive and lets you return to it later. It is the better choice for long interactive work, and is installed in the lab. For anything that should always run, make it a service (chapter 9) rather than a job.
Try it
Check your understanding
- How do you start a command directly in the background?
- You pressed Ctrl+Z in a running command. What state is the job in?
- Which command refers to job number 3?
- Are job numbers (from jobs) the same as PIDs?
- What happens to your background jobs when you close the terminal?
- Ctrl+C in the foreground does what?