6

Can anyone explain the difference between kill and kill -9. Thanks in advance.

tonyjosi
  • 717
  • 1
  • 9
  • 17
  • 3
    Which part of the `kill` man page are you having trouble with? – Ignacio Vazquez-Abrams May 01 '17 at 18:41
  • 1
    First, read `man kill`. Then [Unix_signal - on wikipedia](https://en.wikipedia.org/wiki/Unix_signal), then `man 7 signal`. – clt60 May 01 '17 at 18:55
  • 2
    [StackOverflow's scope](http://stackoverflow.com/help/on-topic) is questions about *writing software* -- specifically, questions "unique to software development"; `kill -9` is frequently used by people not developing software. Consider [SuperUser](https://superuser.com/) or [Unix SE](https://unix.stackexchange.com/). Over there: [Why should I not `kill -9` a process?](https://unix.stackexchange.com/questions/8916/when-should-i-not-kill-9-a-process) – Charles Duffy May 01 '17 at 19:43
  • Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See [What topics can I ask about here](http://stackoverflow.com/help/on-topic) in the Help Center. Perhaps [Super User](http://superuser.com/) or [Unix & Linux Stack Exchange](http://unix.stackexchange.com/) would be a better place to ask. – jww Jun 30 '17 at 21:58

3 Answers3

19

kill aka kill -TERM aka kill -15 is the safe and correct way of terminating a process. It's equivalent to safely shutting down a computer.

kill -9 is the unsafe way of brutally murdering a process. It's equivalent to pulling the power cord, and may cause data corruption.

See the Linux&Unix stack exchange for more information.

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • I like your analogy. For those who want to find out more about SIGTERM and SIGKILL, you might want to check [this](https://www.gnu.org/software/libc/manual/html_node/Termination-Signals.html). – jumping_monkey Oct 09 '20 at 01:47
2

Both Kill and Kill -9 are used to kill a process . But the difference is seen in how the process which received the Kill or Kill -9 behaves.

Kill will generate a SIGTERM signal asking a process to kill itself gracefully i.e , free memory or take care of other child processes. Killing a process using kill will not have any side effects like unreleased memory because it was gracefully killed.

Kill -9 works similarly but it doesn't wait for the program to gracefully die. Kill -9 generates a SIGKILL signal which won't check the state of the process and kills the process immediately.

In coding/programming context
SIGTERM(generated by Kill) can be captured and handled at code level, if the process is still to reach safe state(clear memory or similar activity) and the process may not be killed immediately and need some time to clear resource and then die . Whereas Process cannot ignore the SIGKILL (generated by Kill -9) and will be killed immediately irrespective of the state they are in(this may some time cause some issues but the process is killed for sure). 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.

1

kill is a utility program for sending different signals to one or more processes (usually, for terminating, pausing, continuing and etc. - manipulating processes).

When executing kill without any signal number (sometimes referred to as signal code), default signal number 15 will be used and that is equivalent to SIGTERM signal name.

One other signal number is 9, which is equivalent to SIGKILL signal name.

The difference between kill -15 PID (which is same as kill PID) and kill -9 PID is that first shuts the specified process down gracefully and second does that forcefully, killing the process and all its sub-processes.


Generally, for killing specific process, you can use kill in one of four alternative fashions:

  • kill -signalnumber PID
  • kill -s signalnumber PID
  • kill -SIGNALNAME PID
  • kill -s SIGNALNAME PID

(PID stands for Process ID)

Giorgi Tsiklauri
  • 9,715
  • 8
  • 45
  • 66