SIGTERM vs. SIGKILL

Sending signals to processes using kill on a Unix system is not a new topic for most systems administrators, but I've been asked many times about the difference between kill and kill -9.

Anytime you use kill on a process, you're actually sending the process a signal (in almost all situations - I'll get into that soon). Standard C applications have a header file that contains the steps that the process should follow if it receives a particular signal. You can get an entire list of the available signals on your system by checking the man page for kill.

Consider a command like this:

kill 2563

This would send a signal called SIGTERM to the process. Once the process receives the notice, a few different things can happen:

  • the process may stop immediately
  • the process may stop after a short delay after cleaning up resources
  • the process may keep running indefinitely

The application can determine what it wants to do once a SIGTERM is received. While most applications will clean up their resources and stop, some may not. An application may be configured to do something completely different when a SIGTERM is received. Also, if the application is in a bad state, such as waiting for disk I/O, it may not be able to act on the signal that was sent.

Most system administrators will usually resort to the more abrupt signal when an application doesn't respond to a SIGTERM:

kill -9 2563

The -9 tells the kill command that you want to send signal #9, which is called SIGKILL. With a name like that, it's obvious that this signal carries a little more weight.

Although SIGKILL is defined in the same signal header file as SIGTERM, it cannot be ignored by the process. In fact, the process isn't even made aware of the SIGKILL signal since the signal goes straight to the kernel init. At that point, init will stop the process. The process never gets the opportunity to catch the signal and act on it.

However, the kernel may not be able to successfully kill the process in some situations. If the process is waiting for network or disk I/O, the kernel won't be able to stop it. Zombie processes and processes caught in an uninterruptible sleep cannot be stopped by the kernel, either. A reboot is required to clear those processes from the system.

Printed from: http://rackerhacker.com/2010/03/18/sigterm-vs-sigkill/ .
© Major Hayden 2012.

13 Comments   »

  • dave says:

    I like the way, you explain the difference its very clear. Thanks

  • Major Hayden says:

    Awesome, Dave. I'm glad it helped.

  • Very good explanation, indeed!

    I was wondering, if I want to halt the machine at a given time, like shutdown -h 10:00, will shutdown
    send what signals? Will it kill the running programs DIsgracefully or not? Say I have transmission downloading an .iso, will it be killed/ended adequately?

  • vilpan says:

    Fernando,
    the usual behaviour is to send SIGTERM to all applications, wait for some time and send SIGKILL to applications that are still alive. The timeout between SIGTERM and SIGKILL might vary from system to system. So any running application is given time to TERMinate gracefully and it if it fails to do so within the timeout it will be KILLed.

  • OK thank you that helps quite a bit actually.

  • ramish says:

    what happen if I send to proccesses kill -9 , i have heard that its destructing for memory for the whole system is it true ? ist there any risk of hramful behaviour ?
    thanks

  • Major Hayden says:

    ramish - If the process is in the middle of writing data to the disk when you issue the SIGKILL, it's possible that data may be partially written and might corrupt any data files that application may be writing.

  • ramish says:

    Hello Major Hayden

    thanks 4 feedback, so this is only risk ? no other resouces which are involved are impacted ?
    Im mean memory or other processes in memory, ?

  • ankit says:

    This is nice explanation about kill.
    Thanks

  • notytony says:

    Hi,

    I have got one doubt..when sigkill is sent to a process as you said the process will mostly be killed.But what happens if the process to be killed have some child threads.?Will those threads also be killed or will they be left as zombies?

    Thanks!

  • Carol Bednar says:

    I am debugging some code that has signal handlers declared for SIGTERM and SIGINT. however this process is being sent SIGKILL and i see the SIGTERM signal handler action. Does this mean said process is also receiving a SIGTERM somewhere along the line? SIGKILL implies no signal handling but immediate exit?
    thanks :-)

  • HONG says:

    if i send a SIGKILL signal to a process, then will its children process also be killed?

  • Major Hayden says:

    HONG -

    Yes. However, if one of the child processes is stuck waiting on something (like the disk, for example), sending a SIGKILL to the parent may only kill the parent process.

Trackbacks/Pingbacks

  1. SIGTERM vs. SIGKILL « Ashishkmishra’s Weblog

RSS feed for comments on this post

Leave a Reply

 

  • Welcome! I started this blog as a way to give back to all of the other system administrators who have taught me something in the past. Writing these posts brings me a lot of enjoyment and I hope you find the information useful. If you spot something that's incorrect or confusing, please write a comment and let me know. Drop me a line if there's something you want to know more about and I'll do my best to write a post on the topic.
    -- Major Hayden

    Flattr this