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:
user-vc8f9gd:~ vanilla$ [1]+Done top -l3 mail -s "3 samples from top" matisse@matisse.net user-vc8f9gd:~ vanilla$
Tip
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:
user-vc8f9gd:~ vanilla$ jobs [1]+Stopped vm_stat 5 [2]-Running top -l3 mail -s "3 samples from top" matisse@matisse.net & user-vc8f9gd:~ vanilla$
To bring a job to the foreground:
1. | jobs Run the jobs command to get a list of job numbers . Pick the one you want to bring back to the foreground. |
2. | 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. |
Tip
Once a job is in the foreground, you can stop it with , or with the following method.
To stop a background job:
1. | jobs This shows you the list of all jobs running. |
2. | 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 AquaThe 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:
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$
Tip
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:
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$
Tip
If the suspended job is the one you most recently suspended, you can use bg with no arguments.