Go to the first, previous, next, last section, table of contents.


Job Control

This chapter disusses what job control is, how it works, and how Bash allows you to access its facilities.

Job Control Basics

Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the system's terminal driver and Bash.

The shell associates a job with each pipeline. It keeps a table of currently executing jobs, which may be listed with the jobs command. When Bash starts a job asynchronously (in the background), it prints a line that looks like:

[1] 25647

indicating that this job is job number 1 and that the process ID of the last process in the pipeline associated with this job is 25647. All of the processes in a single pipeline are members of the same job. Bash uses the job abstraction as the basis for job control.

To facilitate the implementation of the user interface to job control, the system maintains the notion of a current terminal process group ID. Members of this process group (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as SIGINT. These processes are said to be in the foreground. Background processes are those whose process group ID differs from the terminal's; such processes are immune to keyboard-generated signals. Only foreground processes are allowed to read from or write to the terminal. Background processes which attempt to read from (write to) the terminal are sent a SIGTTIN (SIGTTOU) signal by the terminal driver, which, unless caught, suspends the process.

If the operating system on which Bash is running supports job control, Bash allows you to use it. Typing the suspend character (typically `^Z', Control-Z) while a process is running causes that process to be stopped and returns you to Bash. Typing the delayed suspend character (typically `^Y', Control-Y) causes the process to be stopped when it attempts to read input from the terminal, and control to be returned to Bash. You may then manipulate the state of this job, using the bg command to continue it in the background, the fg command to continue it in the foreground, or the kill command to kill it. A `^Z' takes effect immediately, and has the additional side effect of causing pending output and typeahead to be discarded.

There are a number of ways to refer to a job in the shell. The character `%' introduces a job name. Job number n may be referred to as `%n'. A job may also be referred to using a prefix of the name used to start it, or using a substring that appears in its command line. For example, `%ce' refers to a stopped ce job. Using `%?ce', on the other hand, refers to any job containing the string `ce' in its command line. If the prefix or substring matches more than one job, Bash reports an error. The symbols `%%' and `%+' refer to the shell's notion of the current job, which is the last job stopped while it was in the foreground. The previous job may be referenced using `%-'. In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a `+', and the previous job with a `-'.

Simply naming a job can be used to bring it into the foreground: `%1' is a synonym for `fg %1', bringing job 1 from the background into the foreground. Similarly, `%1 &' resumes job 1 in the background, equivalent to `bg %1'

The shell learns immediately whenever a job changes state. Normally, Bash waits until it is about to print a prompt before reporting changes in a job's status so as to not interrupt any other output. If the the `-b' option to the set builtin is set, Bash reports such changes immediately (see section The Set Builtin).

If you attempt to exit Bash while jobs are stopped, the shell prints a message warning you that you have stopped jobs. You may then use the jobs command to inspect their status. If you do this, or try to exit again immediately, you are not warned again, and the stopped jobs are terminated.

Job Control Builtins

bg
bg [jobspec]
Place jobspec into the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used.
fg
fg [jobspec]
Bring jobspec into the foreground and make it the current job. If jobspec is not supplied, the current job is used.
jobs
jobs [-lpnrs] [jobspec]
jobs -x command [jobspec]
The first form lists the active jobs. The options have the following meanings:
-l
List process IDs in addition to the normal information
-n
Display information only about jobs that have changed status since you were last notified of their status.
-p
List only the process ID of the job's process group leader.
-r
Restrict output to running jobs.
-s
Restrict output to stopped jobs.
If jobspec is given, output is restricted to information about that job. If jobspec is not supplied, the status of all jobs is listed. If the `-x' option is supplied, jobs replaces any jobspec found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.
kill
kill [-s sigspec] [-n signum] [-sigspec] jobspec
kill -l [sigspec]
Send a signal specified by sigspec or signum to the process named by jobspec. sigspec is either a signal name such as SIGINT (with or without the SIG prefix) or a signal number; signum is a signal number. If sigspec and signum are not present, SIGTERM is used. The `-l' option lists the signal names, or the signal name corresponding to sigspec.
wait
wait [jobspec|pid]
Wait until the child process specified by process ID pid or job specification jobspec exits and report its exit status. If a job spec is given, all processes in the job are waited for. If no arguments are given, all currently active child processes are waited for.
disown
disown [-h] [jobspec ...]
Without options, each jobspec is removed from the table of active jobs. If the `-h' option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If jobspec is not present, the current job is used.
suspend
suspend [-f]
Suspend the execution of this shell until it receives a SIGCONT signal. The `-f' option means to suspend even if the shell is a login shell.

When job control is not active, the kill and wait builtins do not accept jobspec arguments. They must be supplied process IDs.

Job Control Variables

auto_resume
This variable controls how the shell interacts with the user and job control. If this variable exists then single word simple commands without redirects are treated as candidates for resumption of an existing job. There is no ambiguity allowed; if there is more than one job beginning with the string typed, then the most recently accessed job will be selected. The name of a stopped job, in this context, is the command line used to start it. If this variable is set to the value `exact', the string supplied must match the name of a stopped job exactly; if set to `substring', the string supplied needs to match a substring of the name of a stopped job. The `substring' value provides functionality analogous to the `%?' job ID (see section Job Control Basics). If set to any other value, the supplied string must be a prefix of a stopped job's name; this provides functionality analogous to the `%' job ID.


Go to the first, previous, next, last section, table of contents.