Chapter 6. Multitasking

Suppose you are running a command that will take a long time to process. On a single-task system like MS-DOS, you would enter the command and wait for the system prompt to return, telling you that you could enter a new command. In UNIX, however, there is a way to enter new commands in the “foreground” while one or more commands are still running in the “background.” This is called background processing.

When you enter a command as a background process, the shell prompt reappears immediately so that you can enter a new command. The original command will still be running in the background, but you can use the system to do other things during that time. Depending on your system and your shell, you may even be able to log off and let the background process run to completion.

Running a Command in the Background

Running a command as a background process is most often done to free a terminal when you know the command will take a long time to run.

To run a command in the background, add the “&” character at the end of the command line before you press the RETURN key. The shell will then assign and display a process ID number for the command:

% nroff -ms chap1 > chap1.out &
[1] 29890
%

(The nroff program formats documents. It’s a good example because text formatting usually takes a while, so users often do it in the background. See your UNIX documentation for details on nroff.)

The process ID (PID) for this command is 29890. The PID is useful when you want ...

Get Learning the UNIX Operating System, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.