Job Control


Because the UNIX System provides the capability to run commands in the background, you sometimes have two or more commands running at once. There is always one job in the foreground. This may be the shell, when it is prompting you for input, or it may be any other command to which your keyboard input is connected, such as a text editor. In addition, there may be several jobs running in the background at any given time.

Job control is a crucial feature of the modern UNIX shells that was first introduced in csh and is also found in tcsh, ksh, and bash. The commands and syntax are for the most part identical in all four shells. The job control commands allow you to terminate a background job (kill it), suspend a background job temporarily (stop it), resume a suspended job in the background, move a background job to the foreground, and suspend a foreground job.

The jobs command displays a list of all your jobs, such as

 $ jobs [1] +  Running          find /home/jmf -print > files & [2]    Stopped          vi filesplit.tcl [3] -  Stopped          grep supv * | awk -f fixes > data &

The output shows your current foreground and background jobs, as well as jobs that are stopped or suspended. In this example, there are three jobs. The number at the beginning of each line is the job ID. Job 1 (the find command) is running in the background. Jobs 2 and 3 are both stopped. The plus sign (+) indicates the current job (the most recently started or restarted); minus () indicates the one before that.

You can suspend your current foreground job by typing CTRL-Z. This halts the program and returns you to your shell. For example, if you are running a command that is taking a long time, type CTRL-Z to suspend it so that you can do something else. The job will essentially be paused-that is, it will not do anything until you resume the job, but you can resume it at any time.

Suppose you ran the find command, and forgot to tell it to run in the background. You can use CTRL-Z to suspend it, but now you need to tell it to resume. The command bg will cause a job to run in the background. If the job is stopped, it will resume. By default, bg acts on the current job. To resume an older job, refer to it by the job ID:

 $ j obs [1]+Stopped           find /home/jmf -print > files $ bg %1 [1]+find /home/jmf -print > files &

You use the % sign to introduce the job identifier, so %1 refers to job 1.

Similarly, the command fg causes an existing job to run in the foreground. This can be used to resume a suspended job, or to move a background job to the foreground.

You can terminate any of your background or suspended jobs with the kill command. For example,

 $ kill %2

terminates job number 2. Once a job is killed, it is gone-it can’t be resumed.

In addition to the job ID number, you can use the name of the command to tell the shell which job to kill. For instance,

 $ kill %troff

kills a troff job running in the background. If you have two or more troff commands running, this will kill the most recent one.

The stop command halts execution of a background job but doesn’t terminate it, just like CTRL-Z for foreground jobs. The command sequence

 $ stop %find $ fg %find

stops the find command that is running in the background and then resumes executing it in the foreground. The stop command is supported by csh, tcsh, and ksh, but not by bash. In bash, you would use the following command line instead:

 $ kill -s STOP %find

Table 4–2 summarizes the shell job control commands.

Table 4–2: Job Control Commands

Command

Effect

jobs

List all jobs

CTRL-Z

Suspend current (foreground) process

bg %n

Resume stopped job in background

fg %n

Resume job in foreground

stop %n

Suspend background job

In bash, use kill s STOP %n

kill %n

Terminate job




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net