Linux First Steps
computer software
Linux First Steps Linux First Steps
Linux First Steps   Home | Site Map | About Us | Products | Services | News | Contact Us | Links Linux First Steps
Linux First Steps Linux First Steps

Copyright © 2009

Linux® Bible


background might spew output during commands that you run subsequently from that shell. For
example, if output appears from a backgrounded command during a vi session, simply press
Ctrl+L to redraw the screen to get rid of the output.
To avoid having the output appear, you should have any process running in the back-
ground send its output to a file or to null.
Starting Background Processes
If you have programs that you want to run while you continue to work in the shell, you can place
the programs in the background. To place a program in the background at the time you run the
program, type an ampersand (
&
) at the end of the command line, like this:
$ find /usr > /tmp/allusrfiles &
This example command finds all files on your Linux system (starting from
/usr
), prints those file-
names, and puts those names in the file
/tmp/allusrfiles
. The ampersand (
&
) runs that com-
mand line in the background. To check which commands you have running in the background,
use the
jobs
command, as follows:
$ jobs
[1] Stopped (tty output) vi /tmp/myfile
[2] Running find /usr -print > /tmp/allusrfiles &
[3] Running nroff -man /usr/man2/* >/tmp/man2 &
[4]- Running nroff -man /usr/man3/* >/tmp/man3 &
[5]+ Stopped nroff -man /usr/man4/* >/tmp/man4
The first job shows a text-editing command (
vi
) that I placed in the background and stopped by
pressing Ctrl+Z while I was editing. Job 2 shows the
find
command I just ran. Jobs 3 and 4 show
nroff
commands currently running in the background. Job 5 had been running in the shell (fore-
ground) until I decided too many processes were running and pressed Ctrl+Z to stop job 5 until a
few processes had completed.
The plus sign (
+
) next to number 5 shows that it was most recently placed in the background. The
minus sign (
-
) next to number 4 shows that it was placed in the background just before the most
recent background job. Because job 1 requires terminal input, it cannot run in the background. As
a result, it is
Stopped
until it is brought to the foreground again.
To see the process ID for the background job, add a -l (the lowercase letter L) option
to the jobs command. If you type
ps, you can use the process ID to figure out which
command is for a particular background job.
Using Foreground and Background Commands
Continuing with the example, you can bring any of the commands on the jobs list to the fore-
ground. For example, to edit
myfile
again, type:
$ fg %1
As a result, the
vi
command opens again, with all text as it was when you stopped the vi job.
TIP
TIP
TIP
TIP
64
Linux First Steps
Part I