-->
The Process Termination Command: kill
Although these processes are usually well-behaved and well-managed by Linux, sometimes they may go out of control. This can happen if a program hits a bug or a flaw in its internal code or supplied data or if you accidentally enter the wrong command or command option.
Being able to identify these errant processes and then being able to terminate or kill them is an essential piece of knowledge for all Linux users. (Obviously, the world was a less kind and gentle place when the kill command was developed and named.) When you are your own system administrator, as in our case, its doubly important to be able to kill processes that have gone awry or locked up!
The kill command is used to terminate processes that cant be stopped by other means.
Note:
Before going through the following procedure, if its a program that youre stuck in, make sure you cant stop or exit it by pressing Ctrl+C or some other key combination like q.
- 1. Switch to another virtual console and log in as root.
- 2. Run ps -u and identify the offending process. Note the process ID (PID) number for that process. You will use its PID in the next step.
- 3. Use the kill program by typing kill <PID>, where PID is the Process ID you want to kill. Make sure that you have correctly identified the offending process! As root, you can kill any user process, including the wrong one if you misread or mistype the PID.
- 4. Verify that the process has been killed by using ps -u again. You can type ps -u <PID>, which shows you the status of only the specified PID. If theres a null result and youre just given the Linux prompt again, the PID is dead, so go to step 8. However, its best to look at the complete ps -u list if its not too long. Sometimes the offending process reappears with a new PID! If that is the case, go to step 6.
- 5. If the process is still alive and has the same PID, use kills 9 option. Type kill -9 <PID>. Check it in the same way as in step 4. If this does not kill the process, go to step 7. If the process is now dead, go to step 8.
- 6. If the offending process has reappeared with a new PID, that means that its being created automatically by some other process. The only thing to do now is to kill the parent process, which is the true offender! You may also have to kill the parent process when kill -9 does not work.
- 7. Use ps -l to identify the troublesome processs PPID. This is the PID of the parent process. You should check the parents identity more closely by typing ps -u <Parent PID> before going ahead and killing it as described in step 3, using the PID of the parent in the kill command. You should follow through with step 4 and, if necessary, step 5, making sure the parent process has been killed.
- 8. The process is killed. Remember to log off. Do not leave root logged in on virtual consoles because you may forget that the root logins are there.
Note:
Sometimes processes are simply unkillable! In this case, your best bet is shutting down the Linux system and rebooting.
Linux keeps ordinary users (as opposed to root) from killing other users processes (maliciously or otherwise). For instance, if you are an ordinary user and try to kill the init process, which always has PID=1, you will see a message similar to this one:
darkstar:~$ kill 1
kill: (1) - Not owner
Actually, not even root can kill the init process, although there is no error message. The init process is one of those unkillable processes discussed earlier because its such a key process. Thats all for the best!