[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]
System User's Guide: Operating System and Devices

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.

When a job is started in the background with an & (ampersand), the shell prints a line that looks like:

[1] 1234

This 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 1234.

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 bring the job back into 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.
%+ Equivalent to %%.
%- Specifies the previous job.

This shell learns immediately whenever a process changes 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.

Signal Handling

The SIGINT and SIGQUIT signals for an invoked command are ignored if the command is followed by & (ampersand) and the job monitor option is not active. Otherwise, signals have the values that the shell inherits from its parent.

When a signal for which a trap has been set is received while the shell is waiting for the completion of a foreground command, the trap associated with that signal will not be executed until after the foreground command has completed. Therefore, a trap on a CHILD signal is not performed until the foreground job terminates.


[ Next Article | Previous Article | Book Contents | Library Home | Legal | Search ]