Section 9: Interaction and Job Control
When you log in to a UNIX system, the kernel starts a shell for you,
and connects the shell to your terminal. When you execute a command
from the shell, the shell creates a child process to execute the
command, and connects the child process to your terminal. By
connecting the child process to your terminal, the shell allows you to
send input to the child process, and receive output from it. When the
child process finishes, the shell regains access to the terminal,
redisplays the shell prompt, and waits for your next command.
Any task that requires you to actively participate (like word
processing) must be in the foreground to run. Such jobs, termed
"interactive," must periodically update the display, and accept
input from you, and so require access to the terminal interface.
Other jobs do not require you to participate once they are started.
For example, a job that sorts the contents of one file and places the
results in another file, would not have to accept user commands, or
write information to the screen while it runs. Some UNIX shells allow
such noninteractive jobs to be disconnected from the terminal,
freeing the terminal for interactive use.
Note that it is even possible to log out, and leave a background
process running. Unfortunately, there is no way to reconnect a
background job to the terminal after you've logged out.
Jobs that are disconnected from the terminal for input and output are
called "background" jobs. You can have a large number of background
jobs running at the same time, but you can only have one foreground
job. That is because you only have one screen, and one keyboard at
Background and foreground jobs
The process that is connected to the terminal is called the
foreground job. A job is said to be in the foreground because
it can communicate with the user via the screen, and the keyboard.
A UNIX process can be disconnected from the terminal, and allowed to
run in the background. Because background jobs are not
connected to a terminal, they cannot communicate with the user. If
the background job requires interaction with the user, it will stop,
and wait to be reconnected to the terminal.
Jobs that do not require interaction from the user as they run(like
sorting a large file) can be placed in the background, allowing the
user to access the terminal, and continue to work, instead of waiting
for a long job to finish.
Starting a job in the background
To place a job in the background, simply add the ampersand character
(&) to the end of your shell command.
EXAMPLE: To sort a file called "foo", and place the results
in a file called "bar", you could issue the command
sort < foo > bar &
Examining your jobs
The UNIX command jobs allows you to see a list of all the jobs
you have invoked from the current shell. The shell will list the job
ID of each job, along with the status (running, stopped, or
otherwise), and the command that started the job. The shell considers
the most recently-created (or most recently-manipulated) job to be the
current job, marked with a plus sign. Other jobs are referred
to as previous jobs, and are marked with a minus sign.
The commands related to job control will apply to the current job,
unless directed to do otherwise. You may refer to jobs by job ID by
using the percent sign. Thus, job 1 is referred to as %1, job 2
is %2, and so forth.
Suspending the foreground job
You can (usually) tell UNIX to suspend the job that is currently
connected to your terminal by typing Control-Z (hold the control key
down, and type the letter z). The shell will inform you that the
process has been suspended, and it will assign the suspended job a job
There is a big difference between a suspended job, and a job running
in the background. A suspended job is stopped, and no processing will
take place for that job until you run it, either in the foreground, or
in the background.
Placing a foreground job into the background
If you started a job in the foreground, and would like to place it in
the background, the first thing you must do is suspend the job with a
Control-Z, freeing your terminal. Then, you can issue the UNIX
to place the suspended job in the
The bg command can accept a job ID as an argument. If no job ID is
given, bg assumes you are referring to the current (suspended) job.
Bringing a background job to the foreground
You can reconnect a background job to your terminal with the UNIX
The fg command will accept a job ID
as an argument. Make sure to include the percent sign:
will bring job 2 into the foreground. If no job ID is
given, fg will assume you are referring to the current (suspended)
Starting a suspended job
If you have a suspended job that you'd like to resume running, first
you must decide whether you want it running in the foreground, or the
background. Find the job ID of the suspended job with the jobs
command, and then use bg (to run the job in the background), or
fg (to run the job in the foreground).