Frequently Asked Question

How do shell job control commands (Ctrl-Z, fg, bg, jobs, &) work?

The shell organises every command you start into a "job", a single foreground command, a pipeline, or a backgrounded process group, and uses signals to move them between three states: foreground (the shell waits, your keystrokes go to the job), background (the shell prompts, the job runs without input), and stopped (paused). Append & to a command to start it in the background. Press Ctrl-Z to send SIGTSTP to whatever is in the foreground, which suspends it.

Once a job is stopped or backgrounded, jobs lists them with numbers in brackets ([1], [2]); fg %1 resumes job 1 in the foreground; bg %1 resumes it in the background; kill %1 sends SIGTERM to job 1. The flow is so practical that it is worth a moment of muscle-memory practice: edit a file with vim, Ctrl-Z, run a few diagnostic commands, fg to drop straight back into vim with the cursor where you left it.

Video

Further reading and video