Job control in the Korn shell or POSIX shell
The Korn shell, or POSIX shell, provides a facility to control command sequences, or jobs.
When you execute the set -m special command, the Korn shell associates a job with each pipeline. It keeps a table of current jobs, printed by the jobs command, and assigns them small integer numbers.
&), the shell prints a line that looks like the following:
This output indicates that the job, which was started
in the background, was job number 1. It also shows that the job had one (top-level)
process with a process ID of
If you are running a job and want to do something else, use the Ctrl-Z key sequence. This key sequence sends a STOP signal to the current job. The shell normally indicates that the job has been stopped and then displays a shell prompt. You can then manipulate the state of this job (putting it in the background with the bg command), run other commands, and then eventually return the job to the foreground with the fg command. The Ctrl-Z key sequence takes effect immediately, and is like an interrupt in that the shell discards pending output and unread input when you type the sequence.
A job being run in the background stops if it tries to read from the terminal. Background jobs are normally allowed to produce output. You can disable this option by issuing the stty tostop command. If you set this terminal option, then background jobs stop when they try to produce output or read input.
You can refer to jobs in the Korn shell in several ways. A job is referenced by the process ID of any of its processes or in one of the following ways:
|%Number||Specifies the job with the given number.|
|%String||Specifies any job whose command line begins with the String variable.|
|%?String||Specifies any job whose command line contains the String variable.|
|%%||Specifies the current job.|
|%-||Specifies the previous job.|
This shell immediately recognizes changes in the process state. It normally informs you whenever a job becomes blocked so that no further progress is possible. The shell does this just before it prints a prompt so that it does not otherwise disturb your work.
When the monitor mode is on, each completed background job triggers traps set for the CHLD signal.
If you try to leave the shell (either by typing exit or
using the Ctrl-D key sequence) while jobs are stopped or running, the system
warns you with the message
There are stopped (running) jobs.
Use the jobs command to see which jobs are affected. If
you immediately try to exit again, the shell terminates the stopped and running
jobs without warning.