UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 38.9 Killing Foreground Jobs Chapter 38
Starting, Stopping, and Killing Processes
Next: 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script

38.10 Destroying Processes with kill

Sometimes it's necessary to eliminate a process entirely or to signal a process (38.11); this is the purpose of the kill command. You can use the kill command with or without a signal id:

% kill pid
% kill -signal pid

where pid is the process' identification number, and signal (which is optional) is the signal to send to the process. The default signal is number 15, the TERM signal, which tells the process to terminate. Under System V, the signal must be specified numerically; under BSD, either the signal number or its symbolic name may be used. [Use kill -l for a list of signal names; unfortunately, the listing doesn't show the correspondence of names and numbers. However, they are in order, so if you can count, you can figure it out. -TOR ]

Sometimes, a process may still exist after a kill command. If this happens, execute the kill command with the -9 option, which sends the process signal 9, appropriately named KILL. This almost always guarantees that the process will be destroyed. However, it does not allow the dying process to clean up, and therefore may leave the process' files in an inconsistent state.

Occasionally, processes will not die even after being sent the KILL signal. The vast majority of such processes fall into one of three categories:

Killing a process may also kill all of its children. Child processes may not die if they're blocking or "catching" the signal you use-although, as explained above, the KILL signal (9) will usually terminate those processes. Killing a shell can therefore kill all the foreground and stopped background processes initiated from that shell (including other shells). Killing a user's login shell is equivalent to logging the user out. This is a useful (if somewhat painful) way to recover from certain kinds of problems. For example, if a user manages to confuse his editor by mistyping control keys and escape sequences, or enters an infinite loop that he can't terminate by normal means, killing his shell will let him regain control of the situation, possibly at the cost of some work. Use the ps command to determine which process is the offending user's shell. Remember that you must be superuser (1.24) in order to kill someone else's process.

- AF from O'Reilly & Associates' Essential System Administration, Chapter 7

Previous: 38.9 Killing Foreground Jobs UNIX Power ToolsNext: 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script
38.9 Killing Foreground Jobs Book Index38.11 Printer Queue Watcher: A Restartable Daemon Shell Script

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System