Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am always very hesitant to run kill -9, but I see other admins do it almost routinely.

I figure there is probably a sensible middle ground, so:

  1. When and why should kill -9 be used? When and why not?
  2. What should be tried before doing it?
  3. What kind of debugging a "hung" process could cause further problems?
share|improve this question
2  
A good related SO answer. –  jw013 Apr 17 '12 at 6:02
add comment

7 Answers

up vote 147 down vote accepted

Generally, you should use kill -15 before kill -9 to give the target process a chance to clean up after itself. (Processes can't catch or ignore SIGKILL, but they can and often do catch SIGTERM.) If you don't give the process a chance to finish what it's doing and clean up, it may leave corrupted files (or other state) around that it won't be able to understand once restarted.

strace/truss, ltrace and gdb are generally good ideas for looking at why a stuck process is stuck. (truss -u on Solaris is particularly helpful; I find ltrace too often presents arguments to library calls in an unusable format.) Solaris also has useful /proc-based tools, some of which have been ported to Linux. (pstack is often helpful).

share|improve this answer
1  
Thanks. I agree. I guess I'm looking for a more compelling reason to not do it that is easy to convey to other sysadmins, and perhaps also some detailed recipes for debugging. –  Mikel Mar 9 '11 at 11:34
17  
the compelling reason is that if you get in the habit of sending SIGKILL, then when you get to a program which will, for example, corrupt an important database for you or your company, you'll really regret it. kill -9 has its use, as a last-resort terminator, emphasis on last-resort; admins that use it before the last-resort a) do not understand being an admin too well, and b) shouldn't be on a production system. –  Arcege Mar 9 '11 at 12:39
4  
@Mikel Another thing to through it, sometimes it's best to trick an app into cleaning itself up with a signal like SIGQUIT or SIGSEGV if it won't respond to SIGINT/SIGTERM. For example, a full screen 3-D app or even Xorg. Using SIGQUIT, it won't have a chance to clean-up anything, but tricking it into thinking a segment fault happen and it will feel it has no choice but to clean up and exit. –  penguin359 Apr 3 '11 at 11:10
4  
@Arcege Do you think that using a database that corrupts data if killed with -9 is a database worth using after all? iirc, mysql, bdb, pg, etc... all behave well when killed with -9. –  dhruvbird Jan 28 at 6:52
3  
killall -9 java ftw –  dmourati Jan 28 at 7:10
show 6 more comments

Randal Schwartz used to frequently post "Useless use of (x)" on lists. One such post was about kill -9. It includes reasons and a recipe to follow. Here is a reconstructed version (quoted below).

(Quote abomination)

No no no.  Don't use kill -9.

It doesn't give the process a chance to cleanly:

1) shut down socket connections

2) clean up temp files

3) inform its children that it is going away

4) reset its terminal characteristics

and so on and so on and so on.

Generally, send 15, and wait a second or two, and if that doesn't
work, send 2, and if that doesn't work, send 1.  If that doesn't,
REMOVE THE BINARY because the program is badly behaved!

Don't use kill -9.  Don't bring out the combine harvester just to tidy
up the flower pot.

Just another Useless Use of Usenet,

(.signature)

share|improve this answer
4  
Won't the operating system close any open file descriptors (including sockets) when the process terminates? –  Brian Gordon Jan 28 at 5:10
    
Yes it will. But suppose you are killing a server process with clients connected, then the clients won't notice that the server is gone before timeouts. –  Björn Lindqvist Jan 28 at 8:48
4  
Ah yes the old "if it is in any way imperfect you are stupid to use it" argument. –  Timmmm Jan 28 at 19:17
    
Or stupid to use if if the process in question is your company's production –  Warren P Jan 29 at 3:24
add comment

It should always be OK to do kill -9, just like it should always be OK to shutdown by pulling the power cable. It may be anti-social, and leave some recovery to do, but it ought to work, and is a power tool for the impatient.

I say this as someone who will try plain kill (15) first, because it does give a program a chance to do some cleanup -- perhaps just writing to a log "exiting on sig 15". But I won't accept any complaint about ill-behaviour on a kill -9.

The reason: plenty of customers do it to things programmers would prefer then don't. Random kill -9 testing is a good and fair test scenario, and if your system doesn't handle it, your system is broken.

share|improve this answer
    
How do you test for "random kill -9"? When you get kill -9, you are done and finished. –  Karel Bílek Jan 28 at 7:28
9  
@Karel: You test whether your system can recover afterwards, and clean up any mangled transactions that were being processed at the time of SIGKILL. –  Tadeusz A. Kadłubowski Jan 28 at 8:09
4  
It is not OK to do a kill -9 just like it is not OK to pull the plug off. While of course there are situations where you have no choice, this should be a last resort action. Of course, pulling the power cable or kill -9 shouldn't have adverse effects like preventing the application or the OS to restart properly if at all, but shit happens and using the recommended ways (kill [-15]) or regular shutdown will help avoiding the mess that might occur if you routinely interrupt programs and OSes that way. In any case, there is always a risk to lose data regardless of the code robustness. –  jlliagre Jan 28 at 12:51
2  
I suspect what Michael meant by 'OK' is that your program should deal with this situation gracefully, and be able to do some form of cleanup on restart. For instance, cleaning up PID files and so forth, rather than just throwing its toys out of the pram and refusing to start. –  gerryk Jan 28 at 22:58
    
@gerryk They should indeed but the issue is some people will take that answer as a "license to kill -9" whatever the situation and the environment. It is an irresponsible attitude. –  jlliagre Jan 29 at 7:24
add comment

I use kill -9 in much the same way that I throw kitchen implements in the dishwasher: if a kitchen implement is ruined by the dishwasher then I don't want it.

The same goes for most programs (even databases): if I can't kill them without things going haywire, I don't really want to use them. (And if you happen to use one of these non-databases that encourages you to pretend they have persisted data when they haven't: well, I guess it is time you start thinking about what you are doing).

Because in the real world stuff can go down at any time for any reason.

People should write software that is tolerant to crashes. In particular on servers. You should learn how to design software that assumes that things will break, crash etc.

The same goes for desktop software. When I want to shut down my browser it usually takes AGES to shut down. There is nothing my browser needs to do that should take more than at most a couple of seconds. When I ask it to shut down it should manage to do that immediately. When it doesn't, well, then we pull out kill -9 and make it.

share|improve this answer
add comment

Not mentioned in all the other answers is a case where kill -9 doesn't work at all, when a process is <defunct> and cannot be killed:

How can I kill a <defunct> process whose parent is init?

What is for a process and why it doesn't get killed?

So before you attempt to kill -9 a <defunct> process run ps -ef to see what his parent is and attempt the -15(TERM) or -2(INT) and lastly -9(KILL) on his parent.

Note: what ps -ef does.

share|improve this answer
add comment

Never never do a kill -9 1. Also avoid doing a kill on certain processes like mount`. When I have to kill a lot of processes (say for example an X session gets hung and I have to kill all the processes of a certain user), I reverse the order of the processes. For example:

ps -ef|remove all processes not matching a certain criteria| awk '{print $2}'|ruby -e '$A=stdin.readlines; A.reverse.each{|a| puts "kill -9 #{a}"}'|bash

Keep in mind that kill does not stop a process and release its resources. All it does is send a SIGKILL signal to the process; you could wind up with a process that's hung.

share|improve this answer
1  
The downvote was someone else. But which resources are not released? Do you just mean the process can't perform its normal cleanup? What about file locks , semaphores, etc.? Can you elaborate? –  Mikel Mar 9 '11 at 11:39
    
Looks like SysV shared memory and semaphores will have to be cleaned up, at least. archives.postgresql.org/pgsql-general/2006-10/msg01065.php –  Mikel Mar 9 '11 at 20:36
6  
This answer is part confusing and part wrong. kill -9 1 is just ignored under most unices. There's no need to avoid kill -9 for mount, but no point in it either. I don't know what you mean by “reverse the order of the processes”. kill -9 does stop (as in, kill) a process, without giving it a chance to complain, however the killing won't happen immediately if the process is in a non-interruptible system call. Killing a process with kill -9 does release most resources, but not all. –  Gilles Mar 9 '11 at 21:01
add comment

kill -9 works if you have a hung serial port.

share|improve this answer
add comment

protected by Michael Mrozek Jan 28 at 20:44

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.