Newbie - Process Management
Chris Gonnerman
chris.gonnerman at newcenturycomputers.net
Tue Jun 18 08:08:03 EDT 2002
----- Original Message -----
From: "Dan Scholey" <scholeyd at logica.com>
> Hi,
>
> I am hoping you can help me, I am fairly new to python (and Unix) but am
> trying to solve the following problem...
>
> I want to be able to check if a parent process has died.
>
> At the moment I can check that a child process has died using...
>
> status = (os.waitpid(child, os.WNOHANG))[0]
> if status!=0:
>
> I've looked at several manuals and can't find a similar function for
parent
> processes.
> Obviously I know both the parent and child process id ( get_pid() /
> get_ppid() ).
Here is a partial screen-capture from my Linux system. If you are using any
standard Unixoid OS this should work:
<< partial ps listing >>
15321 pts/1 S 0:00 -bash
15377 ? S 0:00 /usr/sbin/httpd -f /etc/httpd/httpd.conf
15521 pts/1 R 0:00 ps ax
office:~ $ python
Python 2.1.2 (#1, Jan 16 2002, 17:04:23)
[GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> import os
>>> os.kill(15321,0)
>>> os.kill(15322,0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 3] No such process
>>>
In other words, PID 15321 exists (I knew it did, it's my shell) but 15322
doesn't. So:
def parentcheck():
try:
os.kill(os.get_ppid(), 0)
return 1
except OSError:
return 0
> I have a workaround of....
>
> ps_line=string.split(commands.getoutput("ps -lp
%s"%my_pid),"\012")
> if string.atoi(string.split(ps_line[1])[4])!=daddy:
>
> ... but I would rather not use ps.
>
> Any ideas?
Hope this helps.
> Cheers
> Dan
Chris Gonnerman -- chris.gonnerman at newcenturycomputers.net
http://newcenturycomputers.net
More information about the Python-list
mailing list