Using the kill Command to Control Processes
The kill command is a basic UNIX system command. You can
communicate with a running process by entering a command into its
interface, such as when you type into a text editor. But some processes
(usually system processes rather than application processes) run without such
an interface, and you need a way to communicate with them as well, so you
use a system of signals. The kill system accomplishes that by sending a
signal to a process, and you can use it to communicate with any process. The
general format of the kill command is as follows:
Click here to view code image
matthew@seymour:~$ kill option PID
Note that if you are using kill on a process you do not own, you need to
have super user privileges and preface the kill command with sudo.
A number of signal options can be sent as words or numbers, but most are of
interest only to programmers. One of the most common is the one used
previously to kill gedit:
Click here to view code image
matthew@seymour:~$ kill PID
This tells the process with PID to stop (where you supply the actual PID).
However, without a signal option, there is no guarantee that a process will be
killed because programs can catch, block, or ignore some terminate signals
(and this is a good thing, done by design). The following example includes a
signal for kill that cannot be caught ( 9 is the number of the SIGKILL
signal):
Click here to view code image
matthew@seymour:~$ kill -9 PID
You can use this combination when the plain kill shown previously does
not work. Be careful, though. Using this does not allow a process to shut
down gracefully, and shutting down gracefully is usually preferred because it
closes things that the process might have been using and ensures that things
such as logs are written before the process disappears. Instead, try this first:
Click here to view code image
matthew@seymour:~$ kill -1 PID
This is the signal to “hang up”—stop—and then clean up all associated
processes as well ( 1 is the number of the SIGHUP signal).