Foreground and background
Normally a command runs in the foreground: the shell waits until it's finished before you get the prompt back. Put & at the end and it runs in the background instead, so you can keep working. The shell prints its job number in square brackets, then its PID:
sleep 300 &
sleep 400 &
jobsIn the terminal
student@vulntech:~$ sleep 300 & [1] 1601 student@vulntech:~$ sleep 400 & [2] 1610 student@vulntech:~$ jobs [1]- Running sleep 300 & [2]+ Running sleep 400 &
jobs lists the background jobs of this shell. The + marks the current job (the one fg and bg use if you don't name one), - the one before.
Ctrl+C and Ctrl+Z
Two keys control the program in the foreground. Ctrl+C interrupts it (the INT signal), which usually ends it. In the examples, ^C means you press Ctrl+C:
sleep 100
^C
echo Back at the promptIn the terminal
student@vulntech:~$ sleep 100 ^C student@vulntech:~$ echo Back at the prompt Back at the prompt
Ctrl+Z (^Z) pauses it instead, and gives you the prompt back. The job is Stopped: still there, but frozen. bg lets it carry on in the background:
sleep 200
^Z
jobs
bg
jobsIn the terminal
student@vulntech:~$ sleep 200 ^Z [1]+ Stopped sleep 200 student@vulntech:~$ jobs [1]+ Stopped sleep 200 student@vulntech:~$ bg [1]+ sleep 200 & student@vulntech:~$ jobs [1]+ Running sleep 200 &
Back to the foreground: fg
fg brings a job back to the foreground, and the shell waits for it again. Choose a job with % and its number. This one only sleeps for 3 seconds, so the prompt comes back by itself:
sleep 3 &
sleep 500 &
fg %1In the terminal
student@vulntech:~$ sleep 3 & [1] 1601 student@vulntech:~$ sleep 500 & [2] 1610 student@vulntech:~$ fg %1 -bash: fg: job has terminated
Job numbers work with kill too, which saves looking up PIDs:
sleep 500 &
sleep 600 &
kill %1
jobsIn the terminal
student@vulntech:~$ sleep 500 & [1] 1601 student@vulntech:~$ sleep 600 & [2] 1610 student@vulntech:~$ kill %1 student@vulntech:~$ jobs [1]- Terminated sleep 500 [2]+ Running sleep 600 &
| You type | What happens |
|---|---|
command & | Run it in the background |
| Ctrl+C | Interrupt the foreground program (usually ends it) |
| Ctrl+Z | Pause the foreground program |
jobs | List this shell's jobs |
bg / bg %2 | Let a paused job continue in the background |
fg / fg %2 | Bring a job to the foreground |
kill %2 | Send TERM to job 2 |
Surviving the end of the session: nohup
When you close a terminal or log out, its jobs get the HUP (hang-up) signal and normally stop. nohup makes a command ignore it, and saves its output in nohup.out because the terminal will be gone:
nohup sleep 900 &
lsIn the terminal
student@vulntech:~$ nohup sleep 900 & [1] 1601 student@vulntech:~$ ls nohup.out
To keep a job you've already started, take it off the shell's list with disown. It keeps running, but jobs no longer knows about it:
sleep 800 &
disown
jobs
pgrep -a sleepIn the terminal
student@vulntech:~$ sleep 800 & [1] 1601 student@vulntech:~$ disown student@vulntech:~$ jobs student@vulntech:~$ pgrep -a sleep 1601 sleep 800
Common mistake: Ctrl+Z doesn't end a program; it only pauses it. If you pressed it by accident, type
fgto get your program back.
Remember:
&runs in the background, Ctrl+Z pauses,bgandfgmove jobs, Ctrl+C interrupts,nohupkeeps a job alive after you leave.