 |
|
Kill a Process
Linux Tips by Burleson Consulting |
When good processes go bad
it is often necessary to terminate them. The kill command is used to send a
termination signal to the specified process or group. The default termination
signal is a SIGTERM (15) signal. A process which receives a SIGTERM may be
programmed to trap signals and perform specific functions or ignore the signal
entirely.
kill takes a process ID as a parameter. The process ID
can be found using any of the methods described earlier in this chapter.
The following example shows the xscreensaver process
(2609) which is to be terminated.
$ pgrep xscreen
2609
$ kill 2609
Though we use pgrep to find the PID of this process here
we could have also used a PID from the output of top or ps.
When there a stubborn process trapping the kill command
and refusing to terminate, the -9 (SIGKILL) signal should be used to kill the
process. The -9 signal cannot be trapped by a process and ignored. If the
xscreensaver process did not terminate after the last kill command, the new kill
command would look like the following:
$ kill -9 2609
While the kill command is typically availble to all
users you will only be able to kill processes you own. To kill processes owned
by other users you will need root privileges.
Kill Processes Using a Pattern
Rather than scanning through process ID (PID) numbers,
it might be easier to kill a process or group of processes by searching for a
pattern in the command name field. The pkill command is one way to accomplish
this, as shown below. pkill matches processes in the same way pgrep does. Be
very careful when using pkill as you could easily kill more than you wanted to!
Here we want to kill all processes called xscreen.
First we use pgrep to make sure we get a reasonable number of PIDs back, then we
kill the process with pkill.
$ pgrep xscreen
2609
$ pkill xscreen
Again, be careful using pkill, especially as root!
Kill All Processes Owned By a Particular User
The pkill command also recognizes the ?u option to look
only at a specific user's processes. By adding the -u option to the pkill
command, it is possible to terminate any and all processes for the specified
user. If, for example, the following command was entered by user terry, all
terry?s active processes would be cancelled, and terry would be kicked off the
system.
$ pkill -9 ?u terry
* Please save any work in progress before trying this!
Since you have the ability to kill any user's processes
this would be a quick way to stop everything they're doing. Be careful though,
doing so may not make you any friends, if you know what I mean.
We've talked mostly about two users so far, root and you
(terry in most of the examples,) but there could be more people than that on our
Linux system! Next we'll look at how to tell who's logged in.
This is an excerpt from "Easy
Linux Commands" by Linux guru Jon Emmons. You can purchase it for only
$19.95 (30%-off) at
this link.