bash: Job Control Basics
1
1 7.1 Job Control Basics
1 ======================
1
1 Job control refers to the ability to selectively stop (suspend) the
1 execution of processes and continue (resume) their execution at a later
1 point. A user typically employs this facility via an interactive
1 interface supplied jointly by the operating system kernel's terminal
1 driver and Bash.
1
1 The shell associates a JOB with each pipeline. It keeps a table of
1 currently executing jobs, which may be listed with the 'jobs' command.
1 When Bash starts a job asynchronously, it prints a line that looks like:
1 [1] 25647
1 indicating that this job is job number 1 and that the process ID of the
1 last process in the pipeline associated with this job is 25647. All of
1 the processes in a single pipeline are members of the same job. Bash
1 uses the JOB abstraction as the basis for job control.
1
1 To facilitate the implementation of the user interface to job
1 control, the operating system maintains the notion of a current terminal
1 process group ID. Members of this process group (processes whose
1 process group ID is equal to the current terminal process group ID)
1 receive keyboard-generated signals such as 'SIGINT'. These processes
1 are said to be in the foreground. Background processes are those whose
1 process group ID differs from the terminal's; such processes are immune
1 to keyboard-generated signals. Only foreground processes are allowed to
1 read from or, if the user so specifies with 'stty tostop', write to the
1 terminal. Background processes which attempt to read from (write to
1 when 'stty tostop' is in effect) the terminal are sent a 'SIGTTIN'
1 ('SIGTTOU') signal by the kernel's terminal driver, which, unless
1 caught, suspends the process.
1
1 If the operating system on which Bash is running supports job
1 control, Bash contains facilities to use it. Typing the SUSPEND
1 character (typically '^Z', Control-Z) while a process is running causes
1 that process to be stopped and returns control to Bash. Typing the
1 DELAYED SUSPEND character (typically '^Y', Control-Y) causes the process
1 to be stopped when it attempts to read input from the terminal, and
1 control to be returned to Bash. The user then manipulates the state of
1 this job, using the 'bg' command to continue it in the background, the
1 'fg' command to continue it in the foreground, or the 'kill' command to
1 kill it. A '^Z' takes effect immediately, and has the additional side
1 effect of causing pending output and typeahead to be discarded.
1
1 There are a number of ways to refer to a job in the shell. The
1 character '%' introduces a job specification (JOBSPEC).
1
1 Job number 'n' may be referred to as '%n'. The symbols '%%' and '%+'
1 refer to the shell's notion of the current job, which is the last job
1 stopped while it was in the foreground or started in the background. A
1 single '%' (with no accompanying job specification) also refers to the
1 current job. The previous job may be referenced using '%-'. If there
1 is only a single job, '%+' and '%-' can both be used to refer to that
1 job. In output pertaining to jobs (e.g., the output of the 'jobs'
1 command), the current job is always flagged with a '+', and the previous
1 job with a '-'.
1
1 A job may also be referred to using a prefix of the name used to
1 start it, or using a substring that appears in its command line. For
1 example, '%ce' refers to a stopped 'ce' job. Using '%?ce', on the other
1 hand, refers to any job containing the string 'ce' in its command line.
1 If the prefix or substring matches more than one job, Bash reports an
1 error.
1
1 Simply naming a job can be used to bring it into the foreground: '%1'
1 is a synonym for 'fg %1', bringing job 1 from the background into the
1 foreground. Similarly, '%1 &' resumes job 1 in the background,
1 equivalent to 'bg %1'
1
1 The shell learns immediately whenever a job changes state. Normally,
1 Bash waits until it is about to print a prompt before reporting changes
1 in a job's status so as to not interrupt any other output. If the '-b'
1 option to the 'set' builtin is enabled, Bash reports such changes
1 immediately (⇒The Set Builtin). Any trap on 'SIGCHLD' is
1 executed for each child process that exits.
1
1 If an attempt to exit Bash is made while jobs are stopped, (or
11 running, if the 'checkjobs' option is enabled - see ⇒The Shopt
Builtin), the shell prints a warning message, and if the 'checkjobs'
1 option is enabled, lists the jobs and their statuses. The 'jobs'
1 command may then be used to inspect their status. If a second attempt
1 to exit is made without an intervening command, Bash does not print
1 another warning, and any stopped jobs are terminated.
1