| Builtins | bg, fg, jobs, kill, disown, suspend, $AUTORESUME
| |
| Variables | to customize job control. |
When a command† is started a terminal process group is
established†
in the foreground.
Keystrokes entered are buffered as type-a-head except for keyboard-generated signals including:
^c (control-c): sigINT
^\ (control-\): sigQUIT
^z: suspend: the process to be suspended immediately.
Pending output and type-a-head to be discarded and control returns to Bash.
The command fg will resume the process group, bg will continue execution in the backgroumd.
^y: delayed suspend:
the process to be suspended when it attempts to read from the terminal then control to be returned to Bash.
& runs asynchronously,
bash associates a job with it and
displays the job number within [ ] and process ID of the last process in the pipeline .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) ).
bg in background
fg in foreground
killed
Currently executing jobs are listed using jobs.
% 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. %?Sum, refers to a job containing the string Sum in its command line.
A job may be referred to using %n.
%% and %+ refer to the current job, which is the last job stopped while it was in the foreground or started in the background.
%- previous job
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 (but not zsh) waits until it is about to output a prompt
before reporting changes in a job's status so as to not interfer with other output, unless
set -b is enabled
trap ' echo "child exited"' CHLD is executed for each child process that exits.
trap ' echo "child exited.\n\n"' CHLD; sleep 3 & ls -lR … [1] + done sleep 3 dgerman@smac14 dgerman % child exited.
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*