Running a Command in the Background
Foreground
All commands up to this point have been run in the foreground. When you run a command in the foreground, the shell waits for it to finish before displaying another prompt and allowing you to continue. When you run a command in the background, you do not have to wait for the command to finish before running another command.
Jobs
A job is another name for a process running a pipeline (which can be a simple command). You can have only one foreground job on a screen, but you can have many background jobs. By running more than one job at a time, you are using one of Linux’s features: multitasking. Running a command in the background can be useful when the command will run for a long time and does not need supervision. It leaves the screen free so you can use it for other work.
Job number, PID number
To run a command in the background, type an ampersand (&; a control operator) just before the RETURN that ends the command line. The shell assigns a small number to the job and displays this job number between brackets. Following the job number, the shell displays the process identification (PID) number—a larger number assigned by the operating system. Each of these numbers identifies the command running in the background. The shell then displays another prompt, and you can enter another command. When the background job finishes, the shell displays a message giving both the job number and the command line used to run the command.
The following example runs in the background; it is a pipeline that sends the output of ls to lpr, which sends it to the printer.
$ ls -l | lpr & [1] 22092 $
The [1] following the command line indicates that the shell has assigned job number 1 to this job. The 22092 is the PID number of the first command in the job. (The TC Shell shows PID numbers for all commands in a job.) When this background job completes execution, you see the message
[1]+ Done ls -l | lpr
(In place of ls –l, the shell might display something similar to ls ––color=auto –l. This difference is due to the fact that ls is aliased [page 352] to ls ––color=auto.)
Moving a Job from the Foreground to the Background
CONTROL-Z and bg
You can suspend a foreground job (stop it from running) by pressing the suspend key, usually CONTROL-Z. The shell then stops the process and disconnects standard input from the keyboard. It does, however, still send standard output and standard error to the screen. You can put a suspended job in the background and restart it by using the bg command followed by the job number. You do not need to specify the job number when there is only one suspended job.
Redirect the output of a job you run in the background to keep it from interfering with whatever you are working on in the foreground (on the screen). Refer to “Control Operators: Separate and Group Commands” on page 299 for more detail about background tasks.
fg
Only the foreground job can take input from the keyboard. To connect the keyboard to a program running in the background, you must bring the program to the foreground. To do so, type fg without any arguments when only one job is in the background. When more than one job is in the background, type fg, or a percent sign (%), followed by the number of the job you want to bring to the foreground. The shell displays the command you used to start the job (promptme in the following example), and you can enter input the program requires to continue.
$ fg 1 promptme
kill: Aborting a Background Job
The interrupt key (usually CONTROL-C) cannot abort a background process because the keyboard is not attached to the job; you must use kill (page 866) for this purpose. Follow kill on the command line with either the PID number of the process you want to abort or a percent sign (%) followed by the job number.
Determining the PID of a process using ps
If you forget a PID number, you can use the ps (process status) utility (page 334) to display it. The following example runs a find command in the background, uses ps to display the PID number of the process, and aborts the job using kill:
$ find / -name memo55 > mem.out & [1] 18228 $ ps | grep find 18228 pts/10 00:00:01 find $ kill 18228 [1]+ Terminated find / -name memo55 > mem.out $
Determining the number of a job using jobs
If you forget a job number, you can use the jobs command to display a list of jobs that includes job numbers. The next example is similar to the previous one except it uses the job number instead of the PID number to identify the job to be killed. Sometimes the message saying the job is terminated does not appear until you press RETURN after the RETURN that executes the kill command.
$ find / -name memo55 > mem.out & [1] 18236 $ bigjob & [2] 18237 $ jobs [1]- Running find / -name memo55 > mem.out & [2]+ Running bigjob & $ kill %1 $ RETURN [1]- Terminated find / -name memo55 > mem.out $