I was looking into a nagios plugin and I found this code:
killall -0 $1
I tried this and it returned 0
for a running process, but it did not kill the process.
Please explain the meaning of this 0
value and why it did not kill the process.
I was looking into a nagios plugin and I found this code:
killall -0 $1
I tried this and it returned 0
for a running process, but it did not kill the process.
Please explain the meaning of this 0
value and why it did not kill the process.
From man 2 kill
:
If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.
kill(2)
returns 0
on success, and -1
otherwise. So, with the zero signal, assuming that the process has enough permission to send a signal to the target process, 0
is returned if the process or process group ID exists, otherwise, it returns -1
(and sets errno
to ESRCH
). Note that the man page simply states that error checking is still performed, so it will return -1
and set errno
to EPERM
if the target process exists but the sending process does not have enough permissions to send it a signal.
The killall(1)
and kill(1)
command utilities behave similarly because they use kill(2)
under the hood.
Note that program code should not rely on this to make decisions based on the existence of a process: there is always a window of time between checking that the process exists and using that information where the process could terminate (and possibly a new, unrelated process starts in that window of time and the same PID is recycled and used).
signal -0
just checks to see if the specified process is running. It doesn't kill it. If you want to kill the process use -9
which is a termination signal.
While -0
isn't listed here, this link explains what some of the other signals are:
http://man7.org/linux/man-pages/man7/signal.7.html
© 2022 - 2024 — McMap. All rights reserved.
SIGKILL
(signal 9), because the process has no chance to do any cleanup before terminating. That should only be used as a last resort when neitherSIGINT
norSIGTERM
seem to work. – Carrolcarroll