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 as a background process is most often done to free a terminal when you know the command will take a long time to run.
nroff -ms chap1 > chap1.out & 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 to check the status of a background process or, if you need to, cancel it. You don't need to remember the PID, because there are UNIX commands (explained in later sections of this chapter) to check on the processes you have running. In some shells, a status line will be printed on your screen when the background process finishes.
In the C shell, you can put an entire sequence of commands separated by semicolons into the background by putting an ampersand at the end of the entire command line. In other shells, enclose the command sequence in parentheses before adding the ampersand:
(command1; command2) &
On many systems, the shells have another feature called job control. You can use the suspend character (usually [CTRL-Z] to suspend a program running in the foreground. The program will pause and you'll get a new shell prompt. You can then do anything else you like, including putting the suspended program into the background using the bg command. The fg command will bring a background process to the foreground.
For example, you might start sort running on a big file, and, after a minute, want to send email. You stop sort, then put it in the background. The shell prints a message, then another shell prompt. You send mail while sort runs.
sort hugefile1 hugefile2 > sorted...time goes by... [CTRL-Z] Stopped %
bg sort hugefile1 hugefile2 > sorted & %