Unix for Mac OS X 10.4 Tiger: Visual QuickPro Guide (2nd Edition)

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:

Tip

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:

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

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 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:

Tip

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:

Tip

Категории