Job Control

Builtins  builtin commands bg, fg, jobs, kill, disown, suspend, autoresume
Variables  Variables Bash uses to customize job control.

Job Control Basics

Job control is the ability to suspend the execution of processes and resume execution at a later time. This is acomplished jointly by the system's terminal driver and Bash.

Bash associates a job with each pipeline. A table of currently executing jobs, is listed using the jobs command.

When Bash starts a job asynchronously ( using an & at the end of the command line), it displays the job number and last process in the pipeline associated with this job for example:

[1] 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.

The operating system maintains a current terminal process group ID, whose members (processes whose process group ID is equal to the current terminal process group ID) receive keyboard-generated signals such as sigINT (by default ^C) and are in the foreground.

Background processes are those whose process group ID differs from the terminal's and do not receive keyboard-generated signals. Background processes which attempt to read from (write to) the terminal are sent a sigTTIN (sigTTOU) signal, which suspends the process, unless previoulsly trapped.

Typing the delayed suspend character (typically ^Y ) while a process is running in the foreground, causes the process to be suspended when it attempts to read input from the terminal then control to be returned to Bash.
Typing the susp character (typically ^Z, Control-Z) causes the process to be suspended and returns control to Bash.
susp takes effect immediately, and causes pending output and typeahead to be discarded.

    The state of this job is then established using
  1. bg to continue it in the background
  2. fg to continue it in the foreground
  3. kill

% introduces a Job name or Job number n may be referred to as %n.

jobs flags the current job with a +, and the previous job with a -.

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.

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 is notified 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 -b option to the set builtin is enabled, Bash reports such changes immediately (see section Set Builtin). Any trap on SIGCHLD is executed for each child process that exits.

Issuing an exit while the are stopped jobs causes Bash to display the warning:
There are stopped jobs. and the exit is not performed.
jobs displays their status.

A second exit causes the stopped jobs are terminated, if there are no intervening commands.

> gzip 0908*
^Z
[1]+ Suspended gzip 0908*
> bg gzip
[1]+ gzip 0908* &
> jobs
[1]+ Running gzip 0908* &
> jobs
[1]+ Done gzip 0908*

Job Control Builtins

bg [jobspec] Resume the suspended job jobspec in the background, as if it had been started with &#amp;. Default the current job.
The return status is zero unless it is run when job control is not enabled, or, if jobspec was not found or specifies a job that was started without job control.

fg [jobspec] Resume the job jobspec in the foreground and make it the current job.
Default: current job.
The return status is that of the command placed into the foreground,
or non-zero if run when job control is disabled or, when run with job control enabled, jobspec does not specify a valid job or a job that was started without job control.

jobs [-lnprs] [jobspec]
jobs -x command [args]
Without options, lists the active jobs.
-l List process IDs in addition to the normal information.

-n Display information only about jobs that have changed status since the user was last notified of their status.
-p List only the process ID of the job's process group leader.
-r running jobs.
-s 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.

-x jobs replaces any jobspec found in command or args with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.

kill [-s signamejobspec
     [-signame]   
or
     [-n signum]   pid
Report signal signame or signum to the process named by jobspec or process ID pid. Default signame: TERM.
shutdown reports quit then term. When job control is not active, kill must be supplied process IDs not jobspec.
kill -l [exit_status] list the names of the signals corresponding to the arguments. Return status is zero.
exit_status is a number specifying a signal number or the exit status of a process terminated by a signal. The return status is zero if at least one signal was successfully sent, or non-zero if an error occurs or an invalid option is encountered.

 1 SIGHUP       2 SIGINT       3 SIGQUIT      4 SIGILL
 5 SIGTRAP      6 SIGABRT      7 SIGEMT       8 SIGFPE
 9 SIGKILL     10 SIGBUS      11 SIGSEGV     12 SIGSYS
13 SIGPIPE     14 SIGALRM     15 SIGTERM     16 SIGURG
17 SIGSTOP     18 SIGTSTP     19 SIGCONT     20 SIGCHLD
21 SIGTTIN     22 SIGTTOU     23 SIGIO       24 SIGXCPU
25 SIGXFSZ     26 SIGVTALRM   27 SIGPROF     28 SIGWINCH
29 SIGINFO     30 SIGUSR1     31 SIGUSR2 
wait [jobspec or
        pid]
With no arguments, all child processes and jobs are waited for. Return status is zero.
With jobspec or pid, wait for it to exit. Return status is that of last command waited for.
If argument does not specify a child process, return status is 127.
When job control is not active, wait must be supplied process IDs not jobspec.

disown [-ar] [-h][jobspec] Each jobspec is removed from the table of active jobs.
-h sigHUP is not sent to the job if the shell receives a sigHUP. current job .
-a all
-r running jobs.
suspend [-f] Suspend the execution of this shell until it receives a sigCONT signal.
-f forces suspend even if the shell is a login shell.

auto_resume If autoresume exists then single_word_simple commands without redirections 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.
top previous section


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