Running a Program in the Background


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 Mac OS X'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) numbera 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.

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 & [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 the job.) When this background job completes execution, you see the message

[1]+ Done      ls -l | lpr 

(If you have set up an alias [page 311] for ls, you will see the alias expansion rather than the command you typed.)

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 268 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 761) 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 & [1] 1966 tcsh $ ps | grep tail  1966  p3 S       0:00.01 tail -f outfile tcsh $ kill 1966 tcsh $ RETURN [1]    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 & [1] 3339 tcsh $ bigjob & [2] 3340 tcsh $ jobs [1]  + Running              tail -f outfile [2]  - Running              bigjob tcsh $ kill %1 RETURN [1]    Terminated           tail -f outfile tcsh $ 

A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: