DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Controlling processes

Killing a process

It is sometimes necessary to kill an executing process. This may be because it is taking up too much process time, causing the system to slow down, or perhaps because it is caught in a loop, and will therefore never complete. To kill the current process, try pressing:

One of these actions should send an interrupt signal to the process (see ``Using signals under the UNIX system'' for more information on signals).

Only the root user can kill processes belonging to another user or to the system.

If the process you want to stop is a child of your shell, you can use the kill(C) command and the process' job number to stop it, as in kill %jobnumber.

If neither the interrupt generation keyboard sequences nor the kill command stop the process, try the following:

  1. Log in to another terminal.

  2. Use ps -u your_login to find out the process ID of the process you want to stop.

  3. Type kill pid to kill the process.
For example:
   $ ps -u charles
   PID	TTY	TIME	COMMAND
   8367	003	0:03	sh
   6800	005	0:03	sh
   26013	005	0:00	find
   26073	003	0:00	ps
   $ kill 26013
   $ ps -u charles
   PID	TTY	TIME	COMMAND
   8367	003	0:03	sh
   6800	005	0:03	sh
   26073	003	0:00	ps
   $
The kill command sends a signal to the target process (26013 in the above example) that causes it to halt. If you run kill without attaching a signal value to it, the signal is given a default value of 15, which is a command to ``terminate,'' which will normally stop a process. If it does not, type kill -9 pid. This is more effective, but does not give the process a chance to close any files it may be working on when it receives the signal.

In theory, if you stop a parent process, you automatically stop all the child processes spawned by it, as follows:

   $ ps -ef | grep charles
   charles 11487     1  0 08:34:15  008      0:07 -ksh
   charles 11514     1  0 08:34:44  009      0:00 -ksh
   charles 11641 11514  1 08:37:41  009      0:05 rm -r *
   charles 11650 11487 11 08:38:32  008      0:00 grep charles
   charles 11651 11487 71 08:38:32  008      0:01 ps -ef
   $ kill -9 11514
   $ ps -ef | grep charles
   charles 11487     1  0 08:34:15  008      0:07 -ksh
   charles 11650 11487 11 08:38:32  008      0:00 grep charles
   charles 11651 11487 71 08:38:32  008      0:01 ps -ef
   $
Note, however, that this procedure runs the risk of corrupting data or causing some other unwanted effect. As a general rule, always explicitly kill a child process before its parent:
   $ ps -ef | grep charles
   charles 11487     1  0 08:34:15  008      0:07 -ksh
   charles 11514     1  0 08:34:44  009      0:00 -ksh
   charles 11641 11514  1 08:37:41  009      0:05 rm -r *
   charles 11650 11487 11 08:38:32  008      0:00 grep charles
   charles 11651 11487 71 08:38:32  008      0:01 ps -ef
   $ kill 11641
   $ kill 11514
   $ ps -ef | grep charles
   charles 11487     1  0 08:34:15  008      0:07 -ksh
   charles 11650 11487 11 08:38:32  008      0:00 grep charles
   charles 11651 11487 71 08:38:32  008      0:01 ps -ef
It is also advisable to try killing a process with the lowest severity signal possible. Only use kill -9 as a last resort.

The status of a killed process is reported as follows:

   [1] + Killed    rm -r *
It is possible to kill all of the currently executing background jobs together, using the kill -p option and some of the special shell parameter notation described in ``Passing arguments to a shell script'', as follows:
   $ kill "$@" $(jobs -p)

Next topic: Suspending a job
Previous topic: Finding out what jobs are running

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003