End a Running Process


kill

Sometimes a program goes rogue, and it won't react to normal attempts to close it. In the GUI, you click repeatedly on the Close button, but nothing happens, or, in the shell, you press Ctrl+C but it fails to stop a running command. When this happens, it's time for the kill command.

You can use several signals with the kill command, ranging from "Please shut yourself down, cleaning up your mess as you do so" to "Shut yourself down as soon as you can" to "Die now!" You're going to focus on the three most important. When you use kill, you can specify the intensity of the process's death with a number or a word, as Table 12.1 shows.

Table 12.1. Common Signals Associated with kill

Signal Number

Signal Word

Meaning

-1

-HUP (hang up)

Controlling process has died. Shut down (if applied to a system service, reload configuration files and restart).

-15

-TERM

Terminate gracefully, cleaning up stray processes and files.

-9

-KILL

Stop whatever you're doing and die now!


Normally, you should first try -15 (in fact, it's the default if you run kill without any option). That way, you give the program a chance to shut down any other programs depending upon it, close files opened by it, and so on. If you've been patient and waited a while ("a while" is completely subjective, of course), and the process is still running completely out of control or still failing to respond, you can bring out the big guns and use -9. The -9 option is equivalent to yanking the rug out from under a running program, and while it does the job, it could leave stray files and processes littering your system, and that's never a good idea.

As for -1, or -HUP, that's primarily for services such as Samba or wireless connectivity. You won't use it much, but you should know what it means.

Here's what you would do if gvim (which normally behaves just fine; this is just for demonstration purposes) seemed to freeze on your system.

$ ps U scott   PID TTY  STAT  TIME COMMAND 14928 ?    S     0:00 /opt/ooo2/program/soffice -writer 14957 ?    Sl    0:42 /opt/ooo2/program/soffice.bin -writer  4688 pts/4 S+   0:00 ssh scott@humbug.machine.com 26751 ?    Z     0:00 [wine-preloader] <defunct> 27921 ?    Ss    0:00 /usr/bin/gvim 27955 pts/5 R+   0:00 ps U scott $ kill 27921 $ ps U scott   PID TTY  STAT  TIME COMMAND 14928 ?    S     0:00 /opt/ooo2/program/soffice -writer 14957 ?    Sl    0:42 /opt/ooo2/program/soffice.bin -writer  4688 pts/4 S+   0:00 ssh scott@humbug.machine.com 26751 ?    Z     0:00 [wine-preloader] <defunct> 27955 pts/5 R+   0:00 ps U scott 


To kill gvim, you use ps to find gvim's PID, which in this case is 27921. Next you kill that PID (and remember that kill uses the TERM signal by default) and then check again with ps. Yes, gvim is no more.

Note

Why didn't you kill PID 26751, which has a STAT of Z, indicating that it's a zombie? Because even -9 doesn't work on a zombieit's already dead, hence the kill command won't work. A reboot is the only way to fix that (usually unimportant) problem.




Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

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