Academic Computing Services * Simon Fraser University

HOW TO

Use Job Control with the Unix C-shell

© March 3, 1995 B-15



The Unix C-shell allows you to run multiple jobs simultaneously, switching your attention from one to another. In simple terms, a job is a task that the Unix system performs. Editing a file and running a utility are both examples of jobs. The job that has your attention is in the foreground. All other jobs are either in the background or suspended. Normally, when you enter a command the resulting job is run in the foreground.

Suspending a job and returning it to the foreground

You may be editing a long file, and decide to send an urgent e-mail message. Rather than closing the file and quitting the editor, it would be convenient to temporarily stop editing, send the message, then return to editing. To suspend a job which is running in the foreground, type Control-z (i.e., hold down the Control key and type z). Unix responds with the message:

Stopped

and the C-shell prompt (%), ready for you to do something else. A suspended (stopped) job is not running, but it is not killed either. It remains quiescent until you bring it back to the foreground.

Return a suspended job to the foreground by typing

fg

In the example above, when you bring the suspended editing job to the foreground, the editor returns to exactly the same state and position as when you suspended it. You may need to redraw the screen after returning certain jobs to the screen (elm sessions, for example). In this case, type Control-l (that's "ell" not "one").

Killing a job in the foreground

Sometimes you may need to terminate a job (perhaps you have typed a command incorrectly or mistaken your position in the file structure). You can kill the job which is running in the foreground by typing Control-c. If this has no effect, suspend the job by typing Control-z and then kill it as a background job, as explained later.

Running a job in the background

Often you'll want to perform other tasks while a lengthy job is running. This is possible by putting the job into the background. The job will continue to run, but you are freed to start additional jobs.

For example, you may want to locate all the files in your directory containing the word "SFU" and put the results in a file. This will take some time, so you wish to run the search in the background. Add an ampersand (&) after the command to indicate that this job should be run in the background:

grep -i "sfu" * > sfufiles &

When you put a job in the background by adding an ampersand, a message similar to the following appears:

[1] 11407

These two numbers are the job number and the PID. They are used to identify the background job (see below). The Unix command prompt (%) then reappears, ready for you to issue other commands. When the background job is completed, you will be informed by a prompt on the screen:

[5] Done grep -i "sfu" * > sfufiles &

You can put a job into the background, and then logout: the job will continue to run.

Identifying jobs

In the C-shell, each job has two identification numbers associated with it. The first, called a job number, is a unique number from the user's perspective. For example, if a user has 3 different jobs running, they will be numbered 1, 2 and 3. The other identification number is called a process identification number (PID, for short). Many users may have multiple jobs running, so the Unix system gives each job a unique PID.

To see what jobs are either stopped or running in the background, type jobs at the command prompt (%). If you have suspended an emacs editing session (using Control-z) and are running the grep command described in the previous section in the background, you will see something like this:

jobs
[1] + Stopped emacs longfile
[2] - Running grep -i "sfu" * > sfufiles

The job command output is in four columns: the job number; the order (+ indicates the current job and - indicates the next most current job); the status (in this case, one job is suspended while the other is running in the background) and the command being executed for each job.

The jobs command calculates the current status of jobs according to the following rules:

The jobs command has a long format option (-l, letter ell not number one) which displays the PID for each job between the order and status information. For example:

jobs -l

might produce the output:

[1]  + 23437 Stopped    emacs longfile

[2]  - 23899 Running    grep -i "sfu" * > sfufiles

Note that the jobs command lists only jobs that have been initiated in this Unix login session; it does not show jobs initiated by other Unix sessions concurrent or previous to this one. (See how-to B-11, Use the Unix clock, for information on leaving jobs running after you have logged out.)

Controlling jobs

When dealing with multiple jobs, you need to be able to put a particular job in the background or bring it to the foreground. You may also need to start or stop a job in the background, or "kill" a job. Job numbers and PID's are used to identify and control jobs.

Moving a job to the foreground

Assume that you have four suspended jobs: editing a file with emacs, compiling a Fortran file, an elm session and an nn session. The result of the jobs command looks like:

jobs -l



[1]    23437 Stopped              emacs longfile

[2]    23588 Stopped (signal)     f77 phexnl.f

[3]  - 24378 Stopped (signal)     elm

[4]  + 24462 Stopped (signal)     nn

Use the fg command to return a job to the foreground. If you use the fg command by itself, it returns the most current job (the one marked with +) to the foreground. To return a different job to the foreground, add the job number with a percent sign to the command. For example, to move the elm session to the foreground, type

fg %3

This command can be abbreviated to just %3.

You can also identify a job by its PID. In our example, we could use the command fg 24378 instead of fg %3.

Handling jobs in the background

Before you can control any of your background jobs, you must first suspend the foreground job with Control-z. In the example above, the Fortran job is stopped in the background. To have it resume running, but leave it in the background, type

bg %2

jobs now shows that job 2 is running, not stopped:

[2]    Running         f77 phexnl.f

To stop a job that is running in the background, use the stop command with the job number. To stop the Fortran job, type

stop %2

A job's PID can also be used with the bg and stop commands.

Killing a job in the background

A previous section described how to kill a job in the foreground using Control-c. This won't work for jobs in the background. To kill a background job, first find its job number using the jobs command, then issue the kill command with the job number or PID. Thus, to kill the Fortran job, type

kill %2 or kill 23588

You can only kill your own processes. Be careful when using kill, especially with text editors, mail programs and other interactive programs. kill terminates a job without saving any of your input or results of the program.

Setting time limits on jobs

Use the limit command to set a time limit for a job, for example, a program you're debugging that might loop endlessly. Note that on Fraser jobs are limited to 10 minutes of CPU time.

Logging out

If you try to logout when there are jobs stopped or running in the background, you will see:

There are stopped jobs.

This reminds you about your stopped jobs so that you can return them to the foreground and exit normally, or kill them. If you ignore the reminder and type logout again, Unix will kill the jobs and log you out.

Summary of Unix job control commands

Control-ckill the foreground job
Control-zsuspend the foreground job
Control-lredraw the screen
&append to command to make it run in background
jobslist stopped and background jobs with job numbers
jobs -llist jobs with process identification number (PID)
fgbring current job to foreground. The current job is marked + in list produced by jobs command.
fg %nbring job number n to foreground (may be abbreviated %n)
bgput current job (marked +) into background, starting it if it is stopped
bg %nput job number n into background, starting it if it is stopped
stop %nstop job number n
kill %nkill job number n


* * * * * * * * * * * * * * *

This page written and maintained by Academic Computing Services, Simon Fraser University.
Please e-mail questions or comments to help@sfu.ca.