Previous Section  < Day Day Up >  Next Section

13.4. Job Control

Job control is a powerful feature of the bash shell that allows you to selectively run programs, called jobs, in the background or foreground. A running program is called a process or a job and each process has a process ID number, called the PID. Normally, a command typed at the command line is running in the foreground and will continue until it has finished unless you send a signal by pressing Ctrl-C or Ctrl-\ to terminate it. With job control, you can send a job to the background and let it keep running; you can stop a job by pressing Ctrl-Z, which sends the job to the background and suspends it; you can cause a stopped job to run in the background; you can bring a background job back to the foreground; and you can even kill the jobs you have running in the background or foreground. For a list of job commands, see Table 13.3 on page 782.

Table 13.3. Job Control Commands

Command

Meaning

bg

Starts running the stopped job in the background

fg

Brings a background job to the foreground

jobs

Lists all the jobs running

kill

Sends the kill signal to a specified job

stop

Suspends a background job

stty tostop

Suspends a background job if it sends output to the terminal

wait [n]

Waits for a specified job and returns its exit status; n is a PID or job number

^Z (Ctrl-Z)

Stops (suspends) the job; the prompt appears on the screen

Argument to Jobs Command

Represents

%n

Job number n

%sting

Job name starting with string

%?string

Job name containing string

%%

Current job

%+

Current job

%–

Previous job, before current job

–r

Lists all running jobs

–s

Lists all suspended jobs


13.4.1 Job Control Commands and Options

By default, job control is already set (some older versions of UNIX do not support this feature). If disabled, it can be reset by any one of the following commands:

FORMAT


set -m           # set job control in the .bashrc file

set -o monitor   # set job control in the .bashrc file

bash -m -i       # set job control when invoking interactive bash


Example 13.27.

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   $ jobs %%

    [2]+ 4538   Running       sleep 25&



6   $ fg %1



7   $ jobs -x echo %1

    4537



8   $ kill %1         # or  kill 4537

    [1]+   Stopped      vi

    Vim: Caught deadly signal TERM

    Vim: Finished.

    [1]+   Exit 1      vi


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 %% argument causes jobs to display the most recent command put in the job table.

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

  7. The –x option can be used to print just the PID number of the job. %1 refers to the vi session that was stopped in the first example.

  8. The kill command sends a TERM signal to the process and kills it. The vi program is killed. You can specify either the job number or the PID number as arguments to the kill command.

New jobs Options

Two new options were added to the jobs command in bash versions 2.x. They are the –r and –s options. The –r option lists all running jobs, and the –s option lists all stopped jobs.

The disown Built-In

The disown built-in command (bash 2.x) removes a specified job from the job table. After the job has been removed, the shell will no longer recognize it as a viable job process and it can only be referenced by its process ID number.

    Previous Section  < Day Day Up >  Next Section