|
|
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:
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:
$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.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 $
In theory, if you stop a parent process, you automatically stop all the child processes spawned by it, as follows:
$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 -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 $
$ 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 -efIt 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 -rIt 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)