Previous Section  < Day Day Up >  Next Section

11.6. Job Control

Job control is used to control the execution of background and foreground jobs.

To use Korn shell job control, the monitor option (set –o monitor) must be set on systems that do not support job control. See Table 11.5 for job control commands.

Table 11.5. Job Control Commands

Command

Function

jobs

Lists all unfinished processes in a numerically ordered list where the number of the job is enclosed in brackets

jobs –l

Same as jobs, but includes the PID number of the job

^Z

Stops the current job

fg %n

Runs background job in foreground

bg %n

Runs job in background

wait %n

Waits for job number n to finish

kill %n

Kills job number n


Example 11.25.

1   $ vi

    [1]  + Stopped           # vi

2   $ sleep 25&

    [2] 4538

3   $ jobs

    [2] +  Running           # sleep 25&

    [1] –  Stopped          # vi

4   $ jobs –l

    [2] + 4538   Running     # sleep 25&

    [1] – 4537   Stopped     # vi

5   $ fg %1


EXPLANATION

  1. After the vi editor is invoked, you can press ^Z (Ctrl-Z) to suspend the vi session. The editor will be suspended in the background, and after the message Stopped appears, the shell prompt will appear immediately.

  2. The ampersand at the end of the command causes the sleep command, with an argument of 25, to execute in the background. The notation [2] means that this is the second job to be run in the background and the PID of this job is 4538.

  3. The jobs command displays the jobs currently in the background.

  4. The jobs command with the –l option displays the processes (jobs) running in the background and the PID numbers of those jobs.

  5. The fg command followed by a percent sign and the job number will bring that numbererd job into the foreground. Without a number, fg brings the most recently backgrounded job back into the foreground.

    Previous Section  < Day Day Up >  Next Section