kill -0 <pid> --- side effect or supported

Ian Lepore ian at freebsd.org
Fri Mar 3 23:17:51 UTC 2017


On Fri, 2017-03-03 at 14:23 -0800, Ngie Cooper (yaneurabeya) wrote:
> > 
> > On Mar 3, 2017, at 14:12, Dirk-Willem van Gulik  wrote:
> > 
> > I regularly use  'kill -0 ' on FreeBSD as  a way to test if a certain process is still running (but without actually sending the signal). And I think it has worked reliably since the mid 80's.
> > 
> > Is it actually a properly supported use - as I recently happened to notice that it does not seem to be all that documented in kill(
> It better work. I have code that relies on it :)…
> 
> It does work as you noted, according to truss:
> 
> # sudo truss -ff kill -0 1 2>&1
> ...
> 79940: kill(1,0)                                 = 0 (0x0)
>> #
> 
> As noted in kill(2), this is one of the valid values:
> 
>      a group of processes.  The sig argument may be one of the signals
>      specified in sigaction(2) or it may be 0, in which case error checking is
>      performed but no signal is actually sent.  This can be used to check the
>      validity of pid.
> 
> So, the manpage for kill(1) is just lacking in the sense that -0 is supported.
> 
> Cheers!
> -Ngie

An interesting point related to "error checking is performed"... one of
the error checks is "do you have permission to send a signal to this
process?"  That means that kill -0 can only be used to check whether a
process you have permission to control is running.  

For example "killall -0 ntpd" cannot tell you whether ntpd is running
unless you are root.

-- Ian


More information about the freebsd-hackers mailing list