Any command or any sequence of commands joined by semicolons or by a pipeline is known as a job. Jobs can be suspended and resumed later or they can run “in the background.” Background jobs are programs which have been executed in such a way that they return the shell prompt to you while they continue to operate, independent of the shell where they were created. The C shell has a number of built-in functions which enable you to control multiple jobs interactively. Note: running jobs in the background can be useful but may put an excessive load on the system if overused. The ‘jobs’ command will give you a list of active jobs.
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  Running cc other.c -o oter (jobs responds) % kill %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  +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 convenient 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 process 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.
& 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)
- You cannot kill processes you don’t own, but you can log in from another terminal and kill a job that has “locked up” your current terminal.
- Killing your login shell (try ‘ps -g’) will log you out. (Remember, you could force yourself off from a different terminal. When you examine the output of ps, look for the ‘-csh’ commands. If you had an edit session active, you might try ‘kill -HUP ###’ as that simulates hanging up the ‘phone’ and most editors will try to preserve interrupted work.)
For further information, see the online manual pages or one of the many general books on using the UNIX files system. IS&T sponsors tutorials on UNIX and other subjects during the academic year and distributes printed handouts on a selection of related subjects.