LINUX
COMMANDS
Contents | Previous
| Next
Job control
Job control can best be illustrated with an example. Enter
the following to start and stop three jobs and list them:
info bash
Ctrl+Z
info ls
Ctrl+Z
info
Ctrl+Z
jobs
...to display:
| [1] |
|
Stopped |
|
info bash |
| [2]- |
|
Stopped |
|
info ls |
| [3]+ |
|
Stopped |
|
info |
Each job (process running in a particular shell) is allocated
a job number. The plus (+) sign indicates
the most recent job and the minus (-) sign indicates
the job before that. Now enter:
fg %1
...to bring job number 1 back to the foreground, then press
Ctrl+Z to stop it again, and enter:
jobs
...to list the jobs again:
| [1]+ |
|
Stopped |
|
info bash |
| [2] |
|
Stopped |
|
info ls |
| [3]- |
|
Stopped |
|
info |
Notice how the most recent (+) and previous (-)
job, has changed.
With the info program you can just bring each
job to the foreground and press q to quit. (By
default fg on its own brings the most recent job
to the foreground.) Do this now to end each of the three
jobs.
Background jobs
Some things you type at the command-line can take a long
time to complete, for instance, enter:
tree -d /usr > usrdirs.txt
...to redirect (>) a tree depiction (tree)
of the directories (-d) in the massive /usr
directory, to the file usrdirs.txt.
Now whilst that's going on, press Ctrl+Z to stop
it, and enter:
bg
...to run the most recent job (the tree command)
in the background, allowing you to continue with other things
in the meantime. (As with fg, you can enter say
bg %2, to run job number 2 in the background.)
Better than the above, is to enter the command, followed
by an ampersand (&) to tell the shell to run the
command in the background:
tree -d /usr > usrdirs.txt &
This you'll often do in a terminal window, to launch an
X program, and allow you to continue typing away. For example:
netscape &
...will run the Netscape Navigator Web browser in the background,
displaying in the terminal window the job number and PID
(process ID) given to the job.
Permanently stopping jobs
To permanently stop the most recent background/stopped
job, you could enter:
kill %
...or to specify the particular background/stopped job,
enter:
kill %2
Much of the time Ctrl+C will also permantely stop
a job running in the foreground, and the most recent background
job can also be stopped with:
fg
Ctrl+C
Note:
Sometimes when you press Ctrl+D to logout you'll
receive a message saying there are stopped jobs. Here a
second press of Ctrl+D will ignore this warning.
Contents | Previous
| Next