Lesson 9.4: Foreground and background jobs
Foreground and background, starting a job with &, listing jobs, stopping the foreground program with Ctrl+C, pausing it with Ctrl+Z, continuing it in the background with bg and bringing it back with fg, job numbers (%1), $!, and keeping a job alive after logout with nohup.
18 min
You will learn to
- Explain foreground and background
- Stop a foreground program with Ctrl+C
- Start background jobs with & and list them with jobs
- Pause with Ctrl+Z and continue with bg or fg
- Refer to jobs as %N and kill them
- Keep a job running after logout with nohup
Key terms
- Foreground
- The program the terminal belongs to right now: it receives your keystrokes, and the shell waits for it to finish before showing a prompt.
- Background
- A program running while the shell is free for other commands. Started with & at the end of the command.
- Job
- A command started from this shell and managed by it, with a job number: %1, %2. Jobs are per shell; ps shows processes system-wide.
- Ctrl+C, Ctrl+Z
- Ctrl+C sends SIGINT to the foreground job (usually ends it). Ctrl+Z sends SIGTSTP (pauses it) and returns the prompt.
- jobs, fg, bg
- jobs lists jobs; fg %N brings one to the foreground; bg %N continues a stopped one in the background.
- nohup
- Runs a command immune to SIGHUP (the 'hang-up' sent at logout), with output to nohup.out.
← Signals: kill, killall and pkill · All lessons · Services with systemd and journalctl →