Job Control
As mentioned earlier, one of the distinctives of Unix is
that it is designed to let multiple individuals run multiple
programs on a single CPU simultaneously. To take advantage
of this, and to juggle multiple tasks effectively, learn the
following commands and syntax.
ps
The computer is doing more things than your realize.
The command ps displays currently running
processes; you can specify the detail desired.
Examples:
- ps     -- show only your processes
in the current window
- ps -f     -- show your processes
in great detail
- ps -ef     -- show all the computers
processes, in great detail
- ps -ef | grep smith     -- show all
processes run by "smith" (in great detail)
The process ID number (PID) can be important.
If you want to kill a program for some reason, and it is running
in the foreground, you can usually do it by typing
^C (control-C). However, sometimes this
doesn't succeed, or perhaps the program is running in the
background (see below) where ^C can't get at it.
In that case, you can find its process ID number and use
kill. Examples:
- kill 3589     -- stop process #3589
- kill -9 3589     -- kill process #3589,
not attempting to do it gracefully (risk of messing up
some files or killing associated processes)
So, if your program myprog is running out of control and you
can't stop it by pressing control-C, you can find its PID
and kill it:
simpson> ps -ef | grep myprog
smithj 17493 1 0 10:18:19 pts/4 0:41 /home/smithj/bin/myprog
simpson> kill -9 17493
&
Some processes can be launched which neither
need further keyboard input nor produce text output.
They can be run in the background, allowing you to
type other commands while the background job keeps running.
Jobs running in the background can continue to run even after
you log off the computer and go home, assuming they don't
require keyboard input or screen display; this is useful for
when you want to run huge calculations on our systems.
Run commands in the background
by typing an ampersand, &,
at the end of the command.
Examples:
- netscape &     -- run Netscape in the
background, so i can type other commands in this window...
- myprog > output.dat &     -- run myprog
in the background, sending any stdout to the file `output.dat'
- myprog > /dev/null &     -- run myprog
in the background, and throw away any stdout
- math -noinit -batchinput -batchoutput < cmmds > rslts &
-- run Mathematica in the background, taking commands
from the file `cmmds' and writing all output to the file
`rslts'
What happens if you forget to type the &?
You don't have to kill the program and retype the command; rather,
you can stop it by typing ^Z (control-Z) to suspend
the process, then type bg to set it running again
in the background.
jobs
Typing the command jobs shows all the
background jobs being run from the current window. If you
want to restore any of them to foreground status,
you type fg job#. (The job number
is not the same as PID.)
simpson> jobs
[1] + Running netscape
[2] - Running myprog
[3] - Running mathematica
simpson> fg 2
myprog
nice
When you run big numbercrunching programs in the background,
the kind which run for hours or days or weeks, you should
precede the command with the word ``nice''.
Then the program will take less of the CPU when
other users log in and run real-time jobs.
Example:
nice math -noinit -batchinput -batchoutput < cmmds > rslts &
-- run Mathematica in the background, taking commands
from the file `cmmds' and writing all output to the file
`rslts', but do it nicely, i.e., without slowing down the
computer for the person sitting at the console and typing
real-time commands