Anchor(JobControl)

Job Control

Though not typically used in scripts, job control is very important in interactive shells. Job control allows you to interact with background jobs, suspend foreground jobs, and so on.

On Posix systems, jobs are implemented as "process groups", with one process being the leader of the group. Each tty (terminal) has a single "foreground process group" that is allowed to interact with the terminal. All other process groups with the same controlling tty are considered background job, and can be either running or suspended. A job is suspended when its process group leader receives one of the signals SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU. Pressing Ctrl-Z sends SIGTSTP to all the processes in the foreground job. SIGTTIN and SIGTTOU are automatically sent whenever a background job tries to read from or write to the terminal---this is why cat & is immediately suspended rather than running in the background.

TODO: Describe job control principles (process groups, sessions, foreground process group, tty signals, etc.) in more detail.

Job control can be turned on with set -m or set -o monitor. Job control enables the following commands:

Other commands for interacting with jobs include:

Anchor(jobspec)

Job Specifications

A job specification or "jobspec" is a way of referring to the processes that make up a job. A jobspec may be:

It is possible to run an arbitrary command with a jobspec, using jobs -x ''cmd args...''. This replaces arguments that look like jobspecs with the PIDs of the corresponding process group leaders, then runs the command. For example, jobs -x strace -p %% will attach strace to the current job (most useful if it is running in the background rather than suspended).

Finally, a bare jobspec may be used as a command: %1 is equivalent to fg %1, while %1 & is equivalent to bg %1.