Given PID, make sure a process is running on Unix?
Michael Hudson
mwh at python.net
Mon Jul 8 07:05:55 EDT 2002
pinard at iro.umontreal.ca (François Pinard) writes:
> [VanL]
>
> > I am trying to get write a python script that on Unix reads a PID from
> > a file and, if that process is still running, exits. If the process is
> > not running, the process would be started. [...] Any help?
>
> You could work along the lines of:
>
> try:
> os.kill(PID, 0)
> except OSError:
> print PID, 'is not running'
> else:
> print PID, 'is running'
>
> Killing a process with signal 0 never kills it. :-)
You might want to check why the kill failed -- if the process exists
but is owned by another user (and you're not root), then you get an
OSError, but with a different errno.
try:
os.kill(PID, 0)
except OSError, err:
if err.err == errno.EPERM:
print PID, 'is running'
else:
print PID, 'is not running'
else:
print PID, 'is running'
Whether this distinction matters is something I can't predict from
here.
Cheers,
M.
--
Structure is _nothing_ if it is all you got. Skeletons _spook_
people if they try to walk around on their own. I really wonder
why XML does not. -- Erik Naggum, comp.lang.lisp
More information about the Python-list
mailing list