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.
I have everything written except the part that makes sure
the process is running. I have poked through the os module,
but I can only seem to find information about the current
process or child processes, not just some arbitrary process.
This would run from cron or as a daemon, sleeping most of
the time.
Any help?
Thanks,
VanL
If your system has a 'proc' device, you could do something like this:
os.path.isdir('/proc/%d' % pid)
Most Unix flavours have a proc device AFAIK.
--
Best Regards
Thomas Jensen
(remove underscore in email address to mail me)
All(?) unix flawors have kill(2)
>>> import os
>>> os.kill(12331,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 3] No such process
>>> os.kill(os.getpid(),0)
>>>
--
Jarkko Torppa, Elisa Internet
> 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. :-)
--
François Pinard http://www.iro.umontreal.ca/~pinard
Thanks,
VanL
> [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