home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


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:

  • Zombies. A process in the zombie state ( 38.16 ) is displayed as Z status in BSD ps displays and as <defunct> under System V . ( 38.5 ) When a process is exiting, it informs its parent of its imminent death; when it receives an acknowledgment, its PID is removed from the process table. A zombie process is one whose total resources have been freed, but the parent process' acknowledgment has not occurred. Usually, init will step in when the parent is gone, but very occasionally this fails to happen. Zombies are always cleared the next time the system is booted and do not adversely affect system performance.

  • Processes waiting for unavailable NFS ( 1.33 ) resources (for example, trying to write to a remote file on a system that has crashed) will not die if sent a KILL signal. Use the QUIT signal (3) or the INT (interrupt) signal (2) to kill such processes.

  • Processes waiting for a device to complete an operation before exiting. Often this means waiting for a tape to finish rewinding.

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 Tools Next: 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script
38.9 Killing Foreground Jobs Book Index 38.11 Printer Queue Watcher: A Restartable Daemon Shell Script

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