How do I check if a pid is running?

John La Rooy larooy at xtar.co.nz
Fri May 10 21:24:26 EDT 2002


os.kill(PID,0)

throws an exception if the pid isn't running
OSError: [Errno 3] No such process
or isn't yours
OSError: [Errno 1] Operation not permitted

 John

On Fri, 10 May 2002 14:08:42 +0100
"moray.grieve" <moray.grieve at ntlworld.com> wrote:

> You could do a system call on unix to get a list of all pids belonging to
> yourself i.e.
> 
> lines = os.popen( "ps -u %s -o pid,comm"%(getpass.getuser()) ).readlines()
> pids = []
> for line in lines:
>     pids.append(string.atoi(string.split(line)[0]))
> 
> 
> You could then search through the list to see if the pid you are interested
> is in there. I use this approach on both Solaris and Red Hat.
> 
> Cheers,
> Moray Grieve
> 
> 
> 



More information about the Python-list mailing list