Running Commands in the Background

Backgrounding commands is often done when you know a command is going to take a long time.

To run a command in the background, simply add the "&" character at the end of the comand line before entering it. In example, the creation of this book:


tille:~>docbook-html.sh abook &
[1] 8671
tille:~>

The system assigns a process ID to the job, and the prompt becomes available for entering new commands. (The docbook-html.sh is a script for converting XML to HTML code. It is a fair example because formatting text can take a long time and is therefor often done in the background.)

This process got the ID 8671 assigned. You can use this jobID to check the status of the process and to manipulate it. Some shells will print a statusline when the job is finished:


[1]+  Exit 1                  docbook-html.sh abook

Many shells offer job control. You can pause (not stop) a program by typing the suspend character, which is usually CTRL-Z. You'll be given a new prompt on which you can enter anything you like. Entering the bg will resume the program running in the background, freeing your terminal. The fg will return a background process to the foreground. Example:


tille:~/training/unix-basics>mozilla 

[1]+  Stopped                 mozilla
tille:~/training/unix-basics>bg
[1]+ mozilla &
tille:~>

Note

When you log out while having suspended jobs, the system will inform you about it and leave it up to you to do something about it.

The GNU screen command, freely available, provides means of letting your jobs run while you're logged out. It emulates a terminal to which you can connect or deconnect at any time. See man screen for more information.