BACKGROUND AND FOREGROUND JOBS
A program can be run in the background by executing its command line with '&' at the end.
For example, you could compile a 'c' program in the background.
% cc other.c -o oter &
%
Then, while the compilation proceeds, you could do some other work, such as edit some files with Emacs.
Later, you might have a new idea and decide that you want to change the source program for your c program to include the new idea. If you don't need the c program to finish its compilation, you could suspend Emacs with ^Z and then stop the compiler by using the kill command.
^Z (Suspend emacs)
Stopped (shell acknowledges)
% jobs
[1] Running cc other.c -o oter (jobs responds)
% kill %1
[1] Killed cc other.c -o oter (kill responds)
While you are at the shell prompt, you could pause to look
for mail before resuming emacs:
% mail
No mail for you (mail responds)
% jobs
[1] +Stopped emacs (jobs responds)
% %1 (resume emacs)
In other words, you can resume a job that is stopped by typing %n where 'n' is its job number (the number that the
'jobs' command prints between [ and ]), and you can kill a
suspended or stopped job. In fact, if you find yourself
caught in a program and don't know how to get out of it, you
can suspend the job and then kill it when you get back to
the shell prompt. The scope of the 'jobs' command is limited to the shell that it runs under. Job numbers are con-
venient references to processes under the control of a single shell.
If you have a job running under another shell that you want to stop, such as at another terminal (for example, you left a job running in the background -- some jobs will keep running), you can use the 'ps' command to find its Process ID (PID, a unique number used by the operating system to identify each process). You can then use the PID to kill the job with the 'kill' command.
% ps
PID TT STAT TIME COMMAND
23444 h4 R 0:00 ps
7158 ic R N 12:33 lisp
%
% kill 7158 (kill a procees by its PID number)
% ps
PID TT STAT TIME COMMAND
23555 h4 R 0:00 ps
%
If you try this method and the job still isn't gone, more
drastic methods are needed. Add the -KILL (0r '-9' on some
systems) flag as in:
% kill -KILL 7158
No program under UNIX can trap the KILL signal. Just 'kill'
sends a -TERM (terminate) signal. This usually works, but
if not, -KILL is a sure hit.
COMMAND SUMMARY
& run in the background
<CTRL> Z suspend the current job
<CTRL> C interrupt (usually kill) current job
(some programs, such as editors trap
this -- so that a typo won't abruptly
terminate your editing session, losing
your most recent, unsaved work.)
<CTRL> D signal end of input
(log off shell, if issued at top level)
jobs display jobs running from this shell
ps display jobs you are running
ps -g display all jobs and login shells also
kill %n kill a job by job number
kill PID kill a job by process id number
kill -KILL PID a sure kill
('kill -9 PID' on some UNIX versions)
NOTES
REFERENCES
For further information, see the online manual pages or one
of the many general books on using the UNIX files system.
Information Technology sponsors tutorials on UNIX and other
subjects during the academic year and distributes printed
handouts on a selection of related subjects.