Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Background Processes

A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.

The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!

The simplest way to start a background process is to add an ampersand (&) at the end of the command.

Running the same ls command as in the foreground example, I use the following:

$ ls ch0*.doc &

On the screen, I see the following:

[1]     20757
$ ch01-1.doc  ch010.doc   ch02.doc    ch03-2.doc  ch04-1.doc
⇒ch040.doc   ch05.doc    ch06-2.doc
ch01-2.doc  ch02-1.doc  ch020.doc   ch03.doc    ch04-2.doc
⇒ch05-1.doc  ch050.doc   ch06.doc
ch01.doc    ch02-2.doc  ch03-1.doc  ch030.doc   ch04.doc
⇒ch05-2.doc  ch06-1.doc  ch060.doc

I can see from the first line of output that the process runs in the background. The output is directed to my screen. If the ls command wants any input (which it does not), it goes into a stop state until I move it into the foreground and give it the data from the keyboard.

That first line contains information about the background process—the job number and process ID. You need to know the job number to manipulate it between background and foreground.

If you run this command yourself, you might notice that you do not get a prompt back after the last line of the directory listing. That’s because the prompt actually appears immediately after the job/pid line, next to ch01-1.doc. You are able to enter a command immediately instead of waiting for ls to finish.

If you press the Enter key now, you see the following:

[1] +  Done                    ls ch0*.doc &
$

The first line tells you that the ls command background process finishes successfully. The second is a prompt for another command.


Tip:  
If you try this command and do not see the completion messages, it might be because your shell has been told not to show it to you. When enabled, those messages are part of process or job monitoring. You can enable monitoring with the following:
set -o monitor

To disable the monitoring messages, you use +o:

set +o monitor

You can also check all the shell options with the following:

set -o

You see a different completion message if an error occurs. There is no file with the name no_such_file in my directory, so if I try to list it with ls, I get an error. The command is

$ ls no_such_file &

resulting in output that looks like

[1]     25389
$ no_such_file: No such file or directory

The first line is the background process information, and the second shows the prompt for the next command and the output from ls—the error message. I get the same error message if I run ls as a foreground process.

If you have process and job monitoring enabled, pressing Enter again results in the following appearing on your screen:

[1] +  Done(2)                 ls no_such_file &
$

The ls command returns a nonzero status. That value (2) is shown after the Done message to inform you that it did not run successfully. Of course, the dollar sign ($) on the next line is the command prompt.

Background Processes That Require Input

If you run a background process that requires input and do not redirect it to read a file instead of the keyboard, the process stops. If you have process and job monitoring enabled, pressing Enter at an empty command prompt or starting a command returns a message. The following is an example of running a command in the background that needs input (using a simple program I created that is not part of UNIX):

          $ i_need_input &

Because this command does not produce any output until you give it input, all you see is the command prompt. Pressing Enter results in a message as follows:

          [1] + Stopped (SIGTTIN)        i_need_input &

On some systems the message looks like the following:

          [1] + Stopped (tty input)      i_need_input &

SIGTTIN (seen in the first example) is a signal (SIG) that tells me the program is waiting for terminal (TT) input (IN). See Chapter 19, “Dealing with Signals,” for more information on signals.

If you get a message like this, you have two choices. You can kill the process and rerun it with input redirected, or you can bring the process to the foreground, give it the input it needs, and then let it continue as a foreground or background process. This chapter explains how to handle either of these choices.


Tip:  
Background Processes That Write Output

Some processes force their output to the screen and go into a stop state if they run in the background and want to write output. These processes display a message like:

[1] + Stopped (SIGTTOU)        i_write &

On some systems the message might display as follows:

[1] + Stopped (tty output)     i_write &

SIGTTOU (seen in the first example) is a signal (SIG) that tells me that the program wants to write output (OU) to the terminal (TT) but is in the background. See Chapter 19 for more information on signals.

If you get a message like this, you have two choices. You can kill the process and rerun it with output redirected, or you can bring the process to the foreground where it writes its output and let it continue as a foreground or background process. This chapter explains how to handle either of those choices.



Previous Table of Contents Next