--> --> Jobs and Job Control -->

Jobs and Job Control

A job is a program which has been started from the command line of a UNIX shell. Since Unix is a multi-tasking system, there may be multiple jobs running at the same time. When the shell starts a program it adds it to the job list, where it maintains the list of currently running jobs and their state (which is either running or suspended), and a number that is used to identify the job.

In addition, there are two different modes in which a job can be running:

  1. In the foreground
  2. In the background.

The default behavior is to run in the foreground where the program is in control of the terminal. A job can also be run in the background, where the user does not have any interaction with it, and the shell retains control of the terminal. A job is put in the background on startup by following the command, arguments, and any input/output redirection with an ampersand (&). If a background job requires any input and there is no input redirection, the job will suspend itself until it is brought to the foreground. Output of a job in the background will go to the screen if not redirected, however full screen applications, such as emacs, vi, trn, and the like will be suspended, until brought to the foreground.

To view the list of current jobs, the shell provides thejobs command. The jobs command lists the job number, the state, and the command as executed (after variable and alias substitution, and any i/o redirection). The shell has several methods to allow you to changes the running mode of the job:

  • If the program is a foreground job, you can suspend the job by pressing CTRL-Z (Control-Z)
  • If the program is a foreground job, you can terminate the job by hitting CTRL-C (Control-C), however if the job is blocking Control-C it will not work.
  • A suspended job (which is always in the background, but not running) can be continued in the foreground by the fg command.
  • A suspended job can be told to run in the background with the bg command.
  • A background command can be terminated with the kill command. (This is a shell command, and differs from the KILL signal discussed in the processes section).

Arguments for each of the above commands, except the kill command, are optional, and apply to the current job, which is marked in the job list with a '+' next to the state. The current job, is the job which most recently changed mode (i.e. was suspended or put in the background). However, a job launched in the background will not be the current job if a suspended job appears on the list. The shell indicates a previous job by a '-' next to the state in the jobs list.

There are several ways to specify a job for the above commands. The most common way is to use the job's number preceded by a percent sign. You can also refer to a job by a sub-string of the command line by typing %?substring. Both the prefix and sub-string must not be ambiguous; that is, in some way the string must be unique to a command on the list. Finally you can indicate the current and previous jobs by "%+" and "%-".

Given the following jobs list:

[1]    Suspended                trn alt.politics.usa.republican
[2]  - Suspended                ftp sunsite.unc.edu
[3]  + Suspended                cc foobar.c >& foobar.compile
[4]    Running                  du -s ~ > usage

the command:

  bg %3

would continue the compiling job in the background, the command:

  %3&

would be the same as 'bg %3', the command:

  fg %1

would continue the 'trn' job in the foreground, the command:

  %1

would be the same as 'fg %1', the command:

  kill %du

would terminate the 'du' job, and the command:

  fg %?suns

would continue the 'ftp' job in the foreground.

How are we doing? Comments on this page?
Office of Information Technology
Office of Information Technology Help Desk Web Site University of Maryland Web Site Office of Information Technology Web Site