Previous | Table of Contents | Next |
Moving a Foreground Process to the Background
In addition to running a process in the background using &, you can move a foreground process into the background. While a foreground process runs, the shell does not process any new commands. Before you can enter any commands, you have to suspend the foreground process to get a command prompt. The suspend key on most UNIX systems is Ctrl+Z.
Tip:
You can determine which key performs which function by using the stty command. By entering$ stty -ayou are shown the following, along with a lot of other information:
intr = ^C; quit = ^\; erase = ^H; kill = ^U; ⇒ eof = ^D; eol = ^@ eol2 = ^@; start = ^Q; stop = ^S; susp = ^Z; ⇒ dsusp = ^Y; reprint = ^R discard = ^O; werase = ^W; lnext = ^VThe entry after susp (^Z in this example) is the key that suspends a foreground process. The character ^ stands for Ctrl. If Ctrl+Z does not work for you, use the stty command as shown previously to determine the key for your system.
When a foreground process is suspended, a command prompt enables you to enter more commands; the original process is still in memory but is not getting any CPU time. To resume the foreground process, you have two choicesbackground and foreground. The bg command enables you to resume the suspended process in the background; the fg command returns it to the foreground. fg is covered in the next section.
For example, you start a long running process. Im using long_running_process for the following example:
$ long_running_process
While it is running, you decide that it should run in the background so your terminal is not tied up. To do that, you press the Ctrl+Z key and see the following (the ^Z is your Ctrl+Z key being echoed):
^Z[1] + Stopped (SIGTSTP) long_running_process $
You are told the job number (1) and that the process is Stopped. You then get a prompt. To resume the job in the background, you enter the bg command as follows:
$ bg [1] long_running_process & $
As a result, the process runs in the background. Look at the last character on the second line, the ampersand (&). As a reminder, the shell displays the ampersand there to remind you that the job is running in the background. It behaves just like a command where you type the ampersand at the end of the line.
By default, the bg command moves the most recently suspended process to the background. You can have multiple processes suspended at one time. To differentiate them, put the job number prefixed with a percent sign (%) on the command line.
In the following example, I start two long running processes, suspend both of them, and put the first one into the background. The next few lines show starting and suspending two foreground processes:
$ long_running_process ^Z[1] + Stopped (SIGTSTP) long_running_process $ long_running_process2 ^Z[2] + Stopped (SIGTSTP) long_running_process2 $
To move the first one to the background, I use the following:
$ bg %1 [1] long_running_process & $
The second process is still suspended and can be moved to the background as follows:
$ bg %2 [2] long_running_process2 & $
The capability to specify which job to perform an action on (move to foreground or background for instance) shows the importance of having job numbers assigned to background processes.
Moving a Background Process to the Foreground (fg Command)
When you have a process that is in the background or suspended, you can move it to the foreground with the fg command. By default, the process most recently suspended or moved to the background moves to the foreground. You can also specify which job, using its job number, you want to make foreground.
Tip:
If youre ever in doubt about which job will be moved to the background or foreground, dont guess. Put the job number on the bg or fg command, prefixed with a percent sign.
Using the long running process in the previous section, a foreground process is suspended and moved into the background in the following example:
$ long_running_process ^Z[1] + Stopped (SIGTSTP) long_running_process $ bg [1] long_running_process & $
You can move it back to the foreground as follows:
$ fg %1 long_running_process
The second line shows you which command you moved back to the foreground. The same thing would have happened if it was moved back to the foreground after being suspended.
Keeping Background Processes Around (nohup Command)
You can prevent a background process from terminating, which is the default action, when you sign off or are disconnected. The nohup command prevents your process from getting the HUP (Hang UP) signal and enables it to continue processing.
The nohup command is simple to usejust add it before the command you actually want to run. Because nohup is designed to run when there is no terminal attached, it wants you to redirect output to a file. If you do not, nohup redirects it automatically to a file known as nohup.out.
Running a process in the background with nohup looks like the following:
$ nohup ls & [1] 6695 $ Sending output to nohup.out
Because I do not redirect the output from nohup, it does it for me. If I redirect the output (nohup ls > results &), I do not see the second message.
After waiting a few moments and pressing Enter, I see the following:
[1] + Done nohup ls & $
You can look at the file nohup.out using cat, more, pg, vi, view, or your preferred tool to see the results.
Previous | Table of Contents | Next |