Previous Table of Contents Next


Module 68
jobs (csh, ksh)

DESCRIPTION

The internal jobs command displays all processes in background or in a suspended state. Jobs are processes you have started in foreground, background, or have suspended. There are three states of jobs:

*   foreground A process whose I/O is attached to your terminal. If a process is executing in foreground, you have to wait until it completes to execute the next command.
*   background A process detached from your terminal. The process is executing while you continue to work at your terminal.
*   suspended A process that is not executing. It is waiting to be moved to foreground or background to continue execution.

If you end a command line with an ampersand (&) the process is immediately placed in background for processing.

If you start a command in foreground, you can suspend it by typing Ctrl-Z. You can place it in background with the bg command. The fg command allows you to reattach to the process in the foreground. A suspended job does not execute. It remains at the same instruction where you suspended it.

COMMAND FORMAT

Following is the general format of the jobs command.

     jobs [ -lnp ]

BSD (Berkeley)
jobs [ -l ]

Options

The following options may be used to control how jobs functions.

-l Lists the process IDs in addition to the normal output.
-n Displays jobs that have stopped or exited since last notified.
-p Displays only the process IDs. Not implemented on some systems.

BSD (Berkeley)
-l Same as SV

OUTPUT

The output of the jobs command provides you with the information you need to control jobs. We'll use the following output to explain the information provided by jobs.

     cj> jobs -l
     +[4]  5931   Stopped         vi myfile
     -[3]  5917   Running         du -s / > /tmp/du.all
      [2]  5898   Done(0)         ls -R | lp

The first column contains the job number enclosed in square brackets. The plus (+) signifies the current job. The current job is the last job placed in background or stopped. The minus (-) signifies the previous job.

The second column contains the process ID (PID). Some systems display this as a default; others require the -l option.

The third column contains the current state of the job. The possible states are:


State Description

Done(n) The job has completed with return code n.
Running The job is executing (processing) in background.
Stopped The job has been suspended from execution.
Terminated The job has been aborted via a signal.

Column four contains the name of the command you entered.


C Shell
The C shell provides an internal command to control notification of changes in job states. The command is notify %job. By entering this command the shell will notify you when the status of the job changes. Normally, the shell notifies you of changes in jobs before presenting the shell prompt.
If the notify shell variable is set, the notify %job command is automatically performed for each job you start.

Korn Shell
The ksh uses the -m option to control the job control feature. This is the "monitor" option. If the -m option is set, you can perform job control. If job control is enabled, you will be notified after each job control related command is performed.

RELATED COMMANDS

Refer to the bg, fg, kill, stop and suspend commands described in modules 9, 51, 70, 125, and 129.

APPLICATIONS

You will need to use the jobs command to list any processes you have suspended or placed in the background. When a background job completes, the shell notifies you between commands. If you try to log out while you have suspended jobs, the shell warns you and does not log you out.

TYPICAL OPERATION

In this activity you use the jobs command to display all suspended and background jobs. Begin at the shell prompt.

1.  Type du -s / > /dev/null & and press Return. This places a fairly long running job in background.
2.  Type vi x and press Return.
3.  Press Ctrl-Z to suspend the vi session.
4.  Now type jobs to display the list of background and suspended jobs.
     cj> jobs
     -[1] 1423    Running        du -s / > /dev/null
     +[2] 1428    Stopped        vi x
5.  To kill the background job type kill %- and press Return. Notice you are notified after a couple of seconds that the process was terminated.
6.  Now type fg to re-enter vi. The suspended vi process is brought back into interactive mode.
7.  Type :q! to exit vi.
8.  Turn to Module 129 to continue the learning sequence.


Previous Table of Contents Next