Portable way to tell if a process is still alive

mblume mblume at socha.net
Tue Dec 29 07:10:06 EST 2009


Am Tue, 29 Dec 2009 12:35:11 +0430 schrieb Laszlo Nagy:
> Suppose we have a program that writes its process id into a pid file.
> Usually the program deletes the pid file when it exists... But in some
> cases (for example, killed with kill -9 or TerminateProcess) pid file is
> left there. I would like to know if a process (given with its process
> id) is still running or not. I know that this can be done with OS
> specific calls. But that is not portable. It can also be done by
> executing "ps -p 23423" with subprocess module, but that is not portable
> either. Is there a portable way to do this?
> 
> If not, would it be a good idea to implement this (I think very
> primitive) function in the os module?
> 
In UNIXish systems, successfully sending the signal 0 to a process means
that a process with this pid is still running. Don't know whether this 
works on Windows. What you still don't know is whether this process is 
actually your process or that the OS has reassigned this pid to something
else.

HTH
Martin



More information about the Python-list mailing list