Previous Section  < Day Day Up >  Next Section

9.6. Job Control

Job control is a powerful feature of the C/TC shell that allows you to run programs, called jobs, in the background or foreground. Normally, a command typed at the command line runs in the foreground, and will continue until it has finished. If you have windows, job control may not be necessary, because you can simply open another window to start a new task. On the other hand, with a single terminal, job control is a very useful feature. For a list of job commands, see Table 9.2. (See "TC Shell Job Control" on page 495 for TC shell enhancements for job control.)

Table 9.2. Job Control Commands

Command

Meaning

jobs

Lists all the jobs running

^Z (Ctrl-Z)

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

bg

Starts running the stopped job in the background

fg

Brings a background job to the foreground

kill

Sends the kill signal to a specified job


9.6.1 The Ampersand and Background Jobs

If you expect a command to take a long time to complete, you can append the command with an ampersand and the job will execute in the background. The C/TC shell prompt returns immediately and now you can type another command. Now the two commands are running concurrently, one in the background and one in the foreground. They both send their standard output to the screen. If you place a job in the background, it is a good idea to redirect its output either to a file or pipe it to a device such as a printer.

Example 9.25.

1   % find . -name core -exec rm {} \; &

2   [1]  543

3   %


EXPLANATION

  1. The find command runs in the background. (Without the –print option, the find command does not send any output to the screen).[a]

    [a] The find syntax requires a semicolon at the end of an exec statement. The semicolon is preceded by a backslash to prevent the shell from interpreting it.

  2. The number in square brackets indicates this is the first job to be run in the background and the PID for this process is 543.

  3. The prompt returns immediately. The shell waits for user input.

9.6.2 The Suspend Key Sequence and Background Jobs

To suspend a program, the suspend key sequence, Ctrl-Z, is issued. The job is now suspended (stopped), the shell prompt is displayed, and the program will not resume until the fg or bg commands are issued. (When using the vi editor, the ZZ command writes and saves a file. Do not confuse this with Ctrl-Z, which would suspend the vi session.) If you try to log out when a job is suspended, the message There are stopped jobs appears on the screen.

9.6.3 The jobs Command

The C/TC shell jobs built-in command displays the programs that are currently active and either running or suspended in the background. Running means the job is executing in the background. When a job is stopped, it is suspended; it is not in execution. In both cases, the terminal is free to accept other commands.

Example 9.26.

(The Command Line)

1   % jobs

2   [1] + Stopped    vi filex

    [2] - Running    sleep 25



3   % jobs -l

    [1] + 355  Stopped     vi filex

    [2] - 356  Running     sleep 25



4   [2] Done sleep 25


EXPLANATION

  1. The jobs command lists the currently active jobs.

  2. The notation [1] is the number of the first job; the plus sign indicates that the job is not the most recent job to be placed in the background; the dash indicates that this is the most recent job put in the background; Stopped means that this job was suspended with ^Z and is not currently active.

  3. The –l option (long listing) displays the number of the job as well as the PID of the job. The notation [2] is the number of the second job, in this case, the last job placed in the background. The dash indicates that this is the most recent job. The sleep command is running in the background.

  4. After sleep has been running for 25 seconds, the job will complete and a message saying that it has finished appears on the screen.

9.6.4 The Foreground and Background Commands

The fg command brings a background job into the foreground. The bg command starts a suspended job running in the background. A percent sign and the number of a job can be used as arguments to the fg and bg commands if you want to select a particular job for job control.

Example 9.27.

1   % jobs

2   [1] + Stopped    vi filex

    [2] - Running    cc prog.c -o prog



3   % fg %1

    vi filex

    (vi session starts)



4   % kill %2

    [2] Terminated   cc prog.c -o prog



5   % sleep 15

    (Press ^z)

    Stopped



6   % bg

    [1] sleep 15 &

    [1] Done    sleep 15


EXPLANATION

  1. The jobs command lists currently running processes, called jobs.

  2. The first job stopped is the vi session, the second job is the cc command.

  3. The job numbered [1] is brought to the foreground. The number is preceded with a percent sign.

  4. The kill command is built-in. It sends the TERM (terminate) signal, by default, to a process. The argument is either the number or the PID of the process.

  5. The sleep command is stopped by pressing ^Z. The sleep command is not using the CPU and is suspended in the background.

  6. The bg command causes the last background job to start executing in the background. The sleep program will start the countdown in seconds before execution resumes.

    Previous Section  < Day Day Up >  Next Section