best way to check if pid is dead?

bukzor workitharder at gmail.com
Wed May 21 16:27:18 EDT 2008


On May 21, 12:13 pm, Roy Smith <r... at panix.com> wrote:
> In article
> <90ecca29-c4d8-4e89-908a-93850d7de... at i76g2000hsf.googlegroups.com>,
>
>  bukzor <workithar... at gmail.com> wrote:
> > Does anyone have a pythonic way to check if a process is dead, given
> > the pid?
>
> > This is the function I'm using is quite OS dependent. A good candidate
> > might be "try: kill(pid)", since it throws an exception if the pid is
> > dead, but that sends a signal which might interfere with the process.
>
> > Thanks.
> > --Buck
>
> The canonical way is to do kill(pid, 0).  If it doesn't throw, the process
> exists.  No actual signal is sent to the process either way.
>
> Of course, the process could exit immediately after the kill() call, so by
> the time you find out it's alive, it's dead.  Such is life.

Thanks! That's exactly what I was looking for. A little more
background:

"If sig is 0 (the null signal), error checking is performed but no
signal is actually sent. The null signal can be used to check the
validity of pid."

Taken from : http://www.opengroup.org/onlinepubs/009695399/functions/kill.html



More information about the Python-list mailing list