Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Given PID, make sure a process is running on Unix?

14 views
Skip to first unread message

VanL

unread,
Jul 6, 2002, 5:31:57 PM7/6/02
to
Hello,

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

Thomas Jensen

unread,
Jul 6, 2002, 6:35:20 PM7/6/02
to
VanL wrote:
> Hello,
>
> 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.

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)

Jarkko Torppa

unread,
Jul 6, 2002, 6:43:21 PM7/6/02
to
In article <3D2770A8.7070900@ob_scure.dk>, Thomas Jensen wrote:
> VanL wrote:
>> Hello,
>>
>> 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.
>
> 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.

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

François Pinard

unread,
Jul 6, 2002, 7:25:47 PM7/6/02
to
[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. :-)

--
François Pinard http://www.iro.umontreal.ca/~pinard


VanL

unread,
Jul 7, 2002, 1:04:11 AM7/7/02
to
Thank you! This is just what I was looking for.

Thanks,

VanL

Michael Hudson

unread,
Jul 8, 2002, 7:05:55 AM7/8/02
to
pin...@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

0 new messages