Sometimes it is necessary to kill a process. The process could not be responding to normal imput, it could be a run-away process (consuming system resources and constantly growing), or it could simply be a zombie. The command to kill a process is.... surprise, surprise.... kill!
Kill has the following usage:
kill [OPTION(s)] PID1 [PID2 ...]
Kill is actually more general than just for "killing" processes. In truth, it is just a mechanism for sending signals to a running process. Many of these signals do result in terminating the process, but not all.
Even though kill does have many other options, by far the most common usage is as follows:
kill -SIGNAL PID1 [PID2 ...]
where "SIGNAL" is either the alpha-numeric or numerical specification of a signal. To list the possible signals that kill can submit, run kill with the "-l" option:
A signal can be sent as either the symbolic name or the signal number. For example, a SIGKILL can be sent as either:
kill -SIGKILL 483
or
kill -9 483
The most commonly used signals are as follows:
SIGHUP - 1
This is the "hang-up" signal. The signal has numerous interpretations by various programs, but most commonly it causes the program to reload its configuration file(s) and/or re-open it's interfaces (files/pipes/etc). It should not terminate the process.
SIGTERM - 15
This is the polite termination signal. This signal tells the program to stop what it's doing and exit cleanly- saving its work and politely asking any controlling process (or user) if it was alright to quit. Basically, the process is told to quit, but not required to obey that request. Because it is polite, it is probably not something that will work on run-away or zombied processes.
SIGKILL - 9
Often called "kill nasty", this is the way to forceably terminate a process. When a process is killed thusly, it has no time to shutdown nicely, it is just terminated flat out. This should be considered a "last resort" to end a process. If you stay with UNIX long enough, you will encounter many kill -9 jokes.
What if you want to kill all processes of a certain name without having to look up each process's PID, the command to use is killall. The usage is very similar to normal kill:
killall -SIGNAL PROCESS_NAME ...
For example, if I wanted to send a SIGKILL (kill nasty) to all Netscape processes (Netscape used to be notorious for having run-away and zombie processes), my command would be
killall -SIGKILL netscape
One of my "claims-to-fame" was that I used to draw a comic strip called "Strenua Inertia" (which loosely meant "working hard at nothing"). I once drew a Halloween strip referencing what we've been talking about today. That strip was Slashdotted (which nearly brought my server to its knees). You can see the strip [here].
PROJECT : Kill Stuff
Spawn several processes and practice killing them in a variety of ways:
Spawn Mozilla. Pretend that it has run-away and must be killed. Locate it's PID and kill it "nasty".
Spawn several XTerms. From some other terminal (such as KDE's Konsole) kill all of them. Use the "terminate" or "polite" signal.
You have just reconfigured the SSH server daemon (sshd). Send it a "hang-up" signal to tell it to reload it's configuration files.