best way to check if pid is dead?

Dan Upton upton at virginia.edu
Wed May 21 15:38:19 EDT 2008


On Wed, May 21, 2008 at 3:02 PM, bukzor <workitharder 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
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I don't know if you would call this pythonic, but the way I do it in linux is:

import os
os.path.exists("/proc/%d"%(pid))

Or, more to the point, I'm usually checking to see if processes I
forked have finished, without just having to do a wait4 on them; in
the case you can do something like

procfile = open("/proc/%d/stat" %(pid))
procfile.readline().split[2]

You can do man proc to see what each of the possible letters means; I
look for Z to find that the process has exited but it's waiting for
its parent to do a wait4.

HTH
-dan



More information about the Python-list mailing list