'What is the difference between kill and kill -9?
Can anyone explain the difference between kill and kill -9. Thanks in advance.
Solution 1:[1]
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.
Solution 2:[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.
Solution 3:[3]
A bit more precise and in-depth answer.
kill
is a utility program for sending different signals to one or more processes (usually, for terminating, pausing, continuing and etc. - manipulating processes).
When issuing kill
without any signal number (or signal code-name), default signal value will be used, which is 15
(equivalent to SIGTERM
).
So, kill 1234
is identical to:
kill -15 1234
;kill -s 15 1234
;kill -SIGTERM 1234
;kill -s SIGTERM 1234
;
One other signal code is 9
, which is equivalent to the code-name SIGKILL
.
kill PID
(orkill -15 PID
orkill -SIGTERM
) will gracefully shut down process;kill -9 PID
(orkill -SIGKILL PID
) will forcefully and unconditionally kill the process and all its sub-processes.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | that other guy |
Solution 2 | Saurabh Agarwal |
Solution 3 | Giorgi Tsiklauri |