Tell me more ×
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 should kill -9 be used?
  2. What should be tried before doing it?
  3. Is there any kind of debugging a "hung" process that could cause further problems?
  4. When should kill -9 never be used?
  5. What bad things can happen if kill -9 is used?
share|improve this question
1  
A good related SO answer. – jw013 Apr 17 '12 at 6:02

3 Answers

up vote 19 down vote accepted

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

strace/truss, ltrace (truss -u on Solaris, where it's a bit more helpful; ltrace I find too often doesn't present arguments to library calls in a usable format), and gdb are generally good ideas for looking at why a stuck program is stuck. There are also useful /proc-based tools on Solaris, some of which have been ported to Linux (pstack is often helpful).

share|improve this answer
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
5  
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
1  
@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

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
Awesome link! Thanks. Love the other examples there too. – Mikel Mar 9 '11 at 12:44
1  
Yeah, that page has significantly influenced my POSIX shell programming. – Shawn J. Goff Mar 9 '11 at 12:45

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: eg.

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 it's 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
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
4  
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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