Running a Command in the Background

Running a Command in the Background

Some commands take a while to run. For example, a command that searches through a large number of files or a command that must read a large amount of input may take more time than you're willing to spend twiddling your thumbs.

It is a simple matter to have a command run in the background and get a shell prompt right away so that you can keep working. The command keeps runningyou can stop it or bring it to the foreground if you likebut you can let the operating system worry about the command while you do other things.

To run a command line in the background:

  • Add the & character at the end of the command line.

    At the end of any command line, you can add the & character so that the entire command line runs in the background.

    The shell shows you the background job number and the process ID number for each command on the command line, and gives you a shell prompt right away.

    Figure 2.36 shows an example of getting three samples from the top command and sending them in e-mail.

    Figure 2.36. Running a command in the background, here you've gotten three samples from the top command and sent them in e-mail. (Hey, please send them to yourself, not the author!)

    Once the job is finished running, the shell displays a notice the next time it gives you a new shell prompt. That is, the shell does not spontaneously notify you, but it waits and displays the notice along with the next shell prompt ( Figure 2.37 ).

    Figure 2.37. The shell notifies you when the job is completed.
     user-vc8f9gd:~ vanilla$ [1]+Done                       top -l3  mail -s "3 samples from top" user-vc8f9gd:~ vanilla$ 


  • If a background job needs input from you, it simply sits and waits patientlypossibly forever. Before putting a job in the background, you should have a good idea of how it behaves normally.

You can have several jobs running in the background, and you can bring any of them back to the foreground, or stop any of them by using the kill command, discussed earlier in this chapter.

To see a list of jobs running in the background:

  • jobs

    The jobs command displays a list of background jobs started from the current shell ( Figure 2.38 ). (Insert gratuitous Steve Jobs joke here.) If you have multiple Terminal windows open , each has its own list of background jobs. Even if a job consists of multiple commands (processes), it has a single job number.

    Figure 2.38. Running the jobs command gives you a list of jobs running in the background.
     user-vc8f9gd:~ vanilla$  jobs  [1]+Stopped                    vm_stat 5 [2]-Running                    top -l3  mail -s "3 samples from top" & user-vc8f9gd:~ vanilla$ 

To bring a job to the foreground:


Run the jobs command to get a list of job numbers . Pick the one you want to bring back to the foreground.

fg % n

Use the fg command (meaning foreground ) to bring the job from the background. You identify the job with % and its job number: %1 for job 1, %2 for job 2, and so on.

The job is now running in the foreground.

If you omit the job number, then the job you most recently put in the background will be brought to the foreground.


  • Once a job is in the foreground, you can stop it with , or with the following method.

To stop a background job:


This shows you the list of all jobs running.

kill % n

where n represents any number, such as

kill %2

This is the same kill command we saw earlier in the chapter, only this time instead of the process ID we are using the job ID. The same options apply here. The kill command by itself sends a hang-up signal to each process in the job, requesting that it quit; kill -9 sends kill signals that can't be ignored, stopping the job in its tracks.

Sometimes you might not realize that a command is going to take a while to finish until after you start it. You might have pressed and find yourself waiting for the job to finish. Or maybe you want to temporarily stop a job, get a shell prompt, do something else, and then return to the job that was stopped. You can suspend the job and get a shell prompt back right away.

A suspended job will be in the background, but it won't keep running; that is, its memory remains active, but it consumes no processor time. You can bring it back to the foreground, or tell it to run in the background, just as if you had started it initially with an & at the end of the command line.

Compare with Aqua

The Unix concept of putting a command in the background is very much like the traditional Mac or Aqua situation where you have an application running in one window and you open a window for a different application. The first application continues to run, and you can get on with other things.

To suspend a job:

  • Figure 2.39 shows what happens when you use to suspend the top command while it is running. The shell shows you the job ID and process ID of the suspended job, and returns you to a shell prompt. The ^Z you see in Figure 2.39 is what is displayed when you press .

    Figure 2.39. Using suspends the top command.
     SharedLibs: num = 93, resident = 22.5M code, 1.57M data, 5.52M LinkEdit MemRegions: num = 3234, resident =142M + 7.07M private, 84.3M shared PhysMem:60.1M wired, 71.8M active,245M inactive,377M used,263M free VM: 2.34G + 45.8M 9564(0) pageins, 0(0) pageouts PID    COMMAND     %CPU    TIME       #TH    #PRTS  #MREGS   RPRVT   RSHRD   RSIZE    VSIZE 439    Mozilla     1.7%    36:35.72     6      87     437    26.6M   25.4M   43.2M     109M 396    AOL Instan  1.7%    31:01.89    10     121     170    10.7M   11.0M   16.2M    79.3M 379    Eudora 5.1  0.0%     7:26.90     7     112     162    6.49M   12.5M   11.2M     100M 356    httpd       0.0%     0:00.13     1       9      65     140K   1.25M    612K    2.38M 329    tcsh        0.0%     0:00.61     1      24      17     508K    676K    996K    5.99M 328    ssh-agent   0.0%     0:01.23     1       9      14      80K    352K    172K    1.29M 327    Terminal    1.7%     0:51.57     5     114     103    2.89M   7.12M   6.08M    68.5M 325    sh          0.0%     0:00.01     1      16      13     164K    640K    556K    1.69M  ^Z   [1]+ 736 Suspended  top user-vc8f9gd:~ vanilla$ 


  • You can bring the job back to the foreground as described above using the fg command. If the job you want to bring back to the foreground is the one you just suspended, use fg by itself with no arguments.

If you have suspended a job and decide you want the job to keep running, but in the background, you can do that, too.

To have a suspended job continue running in the background:

  • bg % n

    For example,

    bg %2

    tells job number 2 to start running again, but to do so in the background . Figure 2.40 shows an example of starting a job, suspending it, running another command, and then starting up the suspended job again in the background.

    Figure 2.40. When you suspend a job, you can restart it in the background using bg %n .
     user-vc8f9gd:~ vanilla$  find /Developer -name "*.htm" > found_files  ^Z [1]    +    Stopped         find /Developer -name "*.htm" >found_files user-vc8f9gd:~ vanilla$  uptime  22:04 up 4 days, 3 mins, 3 users, load averages: 0.25 0.09 0.03 user-vc8f9gd:~ vanilla$ bg %1 [1]    +    find /Developer -name "*.htm" >found_files & user-vc8f9gd:~ vanilla$ 


  • If the suspended job is the one you most recently suspended, you can use bg with no arguments.

Unix for Mac OS X 10. 4 Tiger. Visual QuickPro Guide
Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)
ISBN: 0321246683
EAN: 2147483647
Year: 2004
Pages: 161
Authors: Matisse Enzer

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