Killing Processes


You can usually interrupt a foreground process by pressing <Ctrl+C>, but that does not work with background processes. The command used to terminate a process is called kill, which as it turns out is an unfortunate name for a command that does more than just terminate processes. By design, kill sends a signal to a job (or jobs). That signal is sent as an option (after a hyphen) to a process ID:

 kill  signal_no PID 

For instance, you can send the SIGHUP signal to process 7612 like this:

 kill  1 7612 

Signals are messages. They are usually referenced numerically, as with the ever-popular kill 9 signal, but there are a number of others. The ones you are most likely to use are 1, 9, and 15. These signals can also be referenced symbolically with these names.

Signal 1 is SIGHUP. This is normally used with system processes such as xinetd and other daemons. With these types of processes, a SIGHUP tells the process to hang up, reread its configuration files, and restart. Most applications will just ignore this signal.

Signal 9 is SIGKILL, an unconditional termination of the process. Some admins I know call this "killing with extreme prejudice." The process is not asked to stop, close its files, and terminate gracefully. It is simply killed. This should be your last-resort approach to killing a process, and it works 99% of the time. Only a small handful of conditions will ever ignore the 9 signal.

Signal 15, the default, is SIGTERM, a call for normal program termination. The system is asking the program to wrap it up and stop doing whatever it was doing.

Remember when you suspended a process earlier? That was another signal. Try this to get a feel for how this works. If you are running in an X display, start a digital xclock with a seconds display updated every second:

 xclock  digital  update 1 & 

You should see the second digits counting away. Now find its process ID with ps ax | grep xclock. Pretend the process ID is 12136. Let's kill that process with a SIGSTOP:

 kill  SIGSTOP 12136 

The digits have stopped incrementing, right? Restart the clock:

 kill  SIGCONT 12136 

As you can see, kill is probably a bad name for a command that can suspend a process and then bring it back to life. For a complete list of signals and what they do, look in the man pages with this command:

 man 7 signal 

If you want to kill a process by specifying the symbolic signal, you use the signal name minus the SIG prefix. For instance, to send the 1 signal to xinetd, you could do this instead:

 kill  HUP `cat /var/run/xinetd.pid` 

Note that those are backward single quotes around the previous command string.



Moving to Linux(c) Kiss the Blue Screen of Death Goodbye!
Moving to Linux: Kiss the Blue Screen of Death Goodbye!
ISBN: 0321159985
EAN: 2147483647
Year: 2003
Pages: 247

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net