Job Control

Builtinsbg, fg, jobs, kill, disown, suspend, autoresume
Variablesto customize job control.

When a command is started a terminal process group is established in the foreground and it receives keyboard-generated signals including:

A command with an & at the end of the line runs asynchronously. Bash associates a job with it, displays the job number within [ ] and process ID of the last process in the pipeline and continues executing the job in the background.
For example:

> (wc -l /var/log/system.log; head -1  /var/log/system.log; echo ...; tail -f /var/log/system.log )& 
[2] 15448
     228 /var/log/system.log
Jul 17 03:10:04 smacpro syslogd[173]: ASL Sender Statistics 
...
    ASL Module "com.apple.iokit.power" claims selected messages.
    Those messages may not appear in standard system log files or in the ASL database.
Jul 17 16:30:00 smacpro syslogd[173]: Configuration Notice:
    ASL Module "com.apple.mkb" sharing output destination "/private/var/log/keybagd.log" with ASL Module "com.apple.mkb.internal".
    Output parameters from ASL Module "com.apple.mkb.internal" override any specified in ASL Module "com.apple.mkb".
Background processes attempting to read from (write to) the terminal are sent sigTTIN (sigTTOU) which suspends the process, (unless trapped) ).

Currently executing jobs are listed using jobs.

Jobspec

A job may be referred to using a % and a prefix of the name used to start it or a substring in its command line. For example, %monthly refers to a stopped monthlyProductionSummary job.
Using %?Sum, refers to a job containing the string Sum in its command line.
Matching more than one job is an error.

A job may be referred to using %n.

Naming a job can be used to bring it into the foreground. %n is a synonym for fg %n, bringing job [n] to the foreground.
%1 & resumes job 1 in the background, equivalent to bg %1

The shell is notified whenever a job changes state.
Bash waits until it is about to output a prompt before reporting changes in a job's status so as to not interrupt any other output. If set -b is enabled, Bash reports such changes immediately.
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.

The current job is suffixed with a +, and the previous job with a -. A second exit causes the stopped jobs to be terminated, if there are no intervening commands.

 > find . -name 0908
^Z                   user  enters ^z
[1]+  Suspended               find . -name 0908 0908*
 > bg find . -name 0908
[1]+ find . -name 0908 0908* &
 > jobs
[1]+  Running                 find . -name 0908 0908* &
[1]+  Done                    find . -name 0908 0908*

Job Control Builtins

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

fg [jobspec] Resume 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,
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.
> jobs
[1]+  Running   tail -f /var/log/system.log &  (wd: ~) workingDirectory of job
-l include process IDs.
> jobs -l
[1]+ 11612 Running                 tail -f /var/log/system.log & 
-n only jobs that have changed status
-p only the process ID of the job's process group leader.
> jobs -p
11612
-r running jobs.
-s [jobspec]status of stopped jobs.
With jobspec output is only for that job.
-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 signame] jobspec
     [-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 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 SIGILLegal
 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 

C lib signal information
trap

wait [jobspec |
        pid]
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.

Job Control Variables

auto_resume If auto_resume 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

bash table of contents