Jobs: pause, background & bring back
Some commands take a long time: a big download, a backup, a build. You don't have to sit and wait, or open a second terminal. The shell can pause a running command, let it keep running in the background while you do something else, and bring it back when you want it. This is called job control, and it's built into bash on every Linux.
You will learn
- Ctrl+C (stop) vs Ctrl+Z (pause)
&,jobs,bg,fgand%1-style job numbers- Jobs vs processes:
jobs -l,ps,kill %1 - Surviving logout:
nohupanddisown, and when to use systemd instead
Foreground, stopped, background
A normal command runs in the foreground: it owns your terminal until it finishes. Two key combos change that:
| Keys | Signal | What happens |
|---|---|---|
| Ctrl+C | SIGINT (interrupt) | asks the program to stop. It's gone. |
| Ctrl+Z | SIGTSTP (terminal stop) | pauses the program and gives you the prompt back. It's frozen, not gone. |
sleep 300 # waits 5 minutes… ^Z # Ctrl+Z [1]+ Stopped sleep 300 jobs # [1]+ Stopped sleep 300 bg # let it continue, in the background: [1]+ sleep 300 & fg # bring it back to the foreground
The [1] is the job number. It's only for this shell, and you write it with a %: fg %1, bg %2, kill %1. The + marks the "current" job (the one fg and bg pick with no number), - the one before it.
Start in the background: &
sleep 200 & # [1] 5905 ← job number and process ID (PID) jobs -l # with PIDs ps # the same sleep shows up as a normal process
When a background job finishes, bash tells you just before your next prompt: [1]+ Done sleep 200. A background job that prints things still prints them into your terminal, so redirect its output (> out.txt 2>&1) if it's chatty.
Jobs are processes with a nickname
yes > /dev/null & # a job that keeps one CPU at 100% top # there it is at the top of the list kill %1 # by job number… kill 2981 # …or by PID: same thing
Job numbers are a convenience of your shell. Other terminals and other users only see PIDs. (More on processes and signals in Linux Sysadmin, lesson 6.)
Logging out kills your jobs (unless…)
When your SSH session ends, the shell sends its jobs a SIGHUP ("hang up"), and most programs quit. Two ways around it:
nohup ./report.sh & # ignore the hang-up; output goes to nohup.out disown %1 # already running? tell the shell to forget it
nohup is great for "run this long thing while I go to lunch". Anything that must run permanently, or start again after a reboot, should be a systemd service (Linux Basics, lesson 15). And for long interactive work on a remote server, tmux keeps whole sessions alive (Linux Sysadmin, lesson 10).
This lesson needs Ctrl. Use a keyboard for the practice. The ideas are the same everywhere.
Practice: juggle some jobs 🤹
sleep really waits here, so it makes a good long-running job. There's also a slow script, report.sh, in your home folder.
Quick check
1. You pressed Ctrl+Z on a download. Is it still downloading?
✓ Stopped means frozen. Nothing happens until you bg or fg it.
2. What's the difference between kill %1 and kill 1?
✓ Never forget the % for job numbers.
3. A website must keep running after you log out and after reboots. What do you use?
✓ nohup survives logout but not a reboot, and nobody restarts it if it crashes.