12. Job Control
Contents:
12.1 Job Control: Work Faster, Stop Runaway JobsMultitasking, letting you run more than one program at a time, is one of the great things about UNIX. Before job control, though, you had to decide ahead of time whether you wanted to run a job in the foreground (on your screen) or in the background (where you couldn't touch the program except to terminate it before it finished). The C shell - and other shells since it, including some new Bourne shells - have job control built into them. You can start and stop jobs, pop them in and out of the background, and more. Windowing systems, which let you have multiple terminal windows active on the screen at the same time, make this less essential. Still, there are some important areas where you'll get more productivity out of job control than from simply opening another window. This article is an introduction to job control - there's more to learn. Job control takes more than a shell to work right: the UNIX kernel has to support it. Berkeley UNIX since BSD 4.0 has had job control, so most Berkeley-type UNIXes will have it, too. Most versions of UNIX System V before Release 4 did not have job control. If your UNIX doesn't support job control, you can still put a job in the background - see the last paragraph in this article. 12.1.1 Foreground and BackgroundUNIX distinguishes between foreground and background programs. This feature allows you to run several programs simultaneously from your terminal. When a program is running in the foreground, anything you type at the keyboard is sent to the program's standard input unless you have redirected it. As a result, you can't do anything else until the program finishes. When you run a program in the background, it is disconnected from the keyboard. Anything you type reaches the UNIX shell and is interpreted as a command. Therefore, you can run many programs simultaneously in the background. You can run only one program at a time in the foreground.
To run a program in the background, type an ampersand (
%
This runs a FORTRAN compilation in the background, letting you
continue other work while the compilation proceeds. The shell
responds by printing a job number in brackets (
%
To bring a program from the background into the foreground, use the
foreground command,
fg
. If you have more than one background
job, follow
fg
with a job identifier - a percent sign
(
%
The plus sign ( To suspend a job running in the foreground, press CTRL-z. [You can use this to stop most frozen or runaway programs until you figure out what to do next. Also, CTRL-z can stop programs that interrupt characters ( 5.9 ) like CTRL-c can't. -JP ] Entering the background command, bg , lets a stopped program continue execution in the background. The foreground command, fg , restores this program to execution in the foreground. For example:
%
There is no prompt after the
f77
command because the compiler
is running in the foreground. After you press
CTRL-z,
the shell prints the word "Stopped" to indicate that it has stopped
execution. At this point, you can enter any command; the
bg
command lets the job continue executing in the background. This
feature is useful if you forget to type an To terminate a background job, you can use the command's job number rather than its PID number, as follows:
% If you omit it, UNIX interprets the job number as a process number. This will probably be the process number of some operating system function. UNIX will not let you make such a mistake unless you are superuser ( 1.24 ) . If you are superuser, the command is fatal. You may be superuser from time to time and therefore should not develop sloppy habits. In the next few seconds, press RETURN a few times. You should see the message:
[1] Terminated f77 -o program program.F If you don't see that, use the jobs command to check whether the job is still running. If it's still running, use the -9 option as a last resort:
% The -9 option doesn't give the process a chance to clean up its temporary files and exit gracefully, so don't use it unless you need to.
A program running in the background cannot read input from a
terminal. If a background job needs terminal input, it will stop;
the
jobs
command will print the message
On systems and shells without job control features, an - from O'Reilly & Associates' UNIX for FORTRAN Programmers , Chapter 1 |
|