|< Day Day Up >|
In all the examples so far in this book, commands were run in the foreground. When you run a command in the foreground, the shell waits for it to finish before giving you 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 you start running another command.
A job is a series of one or more commands that can be connected by pipes. You can have only one foreground job in a window or 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 important 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 that you can use it for other work. Of course, when you are using a GUI, you can open another window to run another job.
Job number, PID number
To run a command in the background, type an ampersand (&) 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. Then the shell displays another prompt and you can enter another command. When the background job finishes running, the shell displays a message giving both the job number and the command line used to run the command.
Figure 5-15. Using tee
$ who | tee who.out | grep root root console Mar 24 05:00 $ cat who.out root console Mar 24 05:00 alex pts/4 Mar 24 12:23 alex pts/5 Mar 24 12:33 jenny pts/7 Mar 23 08:45
The following examples use the Bourne Again Shell. The TC Shell produces almost identical results. The next example runs in the background and sends its output through a pipe to lpr, which sends it to the printer.
$ ls -l | lpr &  22092 $
The  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 the job.) When this background job completes execution, you see the message
+ Done ls -l | lpr
(In place of ls l, the shell may display something similar to ls color=tty l. This difference is due to the fact that ls is aliased [page 312] to ls color=tty.)
Moving a Job from the Foreground to the Background
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. 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 use the job number when there is only one stopped job.
Only the foreground job can take input from the keyboard. To connect the keyboard to a program running in the background, you must bring it into the foreground. 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 into the foreground. The shell displays the command you used to start the job (promptme in the following example), and you can enter any input the program requires to continue:
bash $ fg 1 promptme
Redirect the output of a job you run in the background to keep it from interfering with whatever you are doing on the screen. Refer to "Separating and Grouping Commands" on page 267 for more detail about background tasks.
kill: Aborting A Background Job
The interrupt key (usually CONTROL-C) cannot abort a process you are running in the background; you must use kill (page 693) 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 a PID number with ps
If you forget the PID number, you can use the ps (process status) utility (page 293) to display it. Using the TC Shell, the following example runs a tail f outfile command (the f option causes tail to watch outfile and display new lines as they are written to the file) as a background job, uses ps to display the PID number of the process, and aborts the job with kill. The same commands work under bash. So that it does not interfere with anything on the screen, the message saying that the job is terminated does not appear until you press RETURN after the RETURN that ends the kill command:
tcsh $ tail -f outfile &  22170 tcsh $ ps | grep tail 22170 pts/7 00:00:00 tail tcsh $ kill 22170 tcsh $ RETURN  Terminated tail -f outfile tcsh $
If you forget the job number, you can use the jobs command to display a list of job numbers. The next example is similar to the previous one but uses the job number instead of the PID number to kill the job:
tcsh $ tail -f outfile &  3339 tcsh $ bigjob &  3340 tcsh $ jobs  + Running tail -f outfile  - Running bigjob tcsh $ kill %1 Terminated tail -f outfile tcsh $
|< Day Day Up >|