[Tutor] Python equivalent of "kill -0 PID"

Martin Walsh mwalsh at mwalsh.org
Thu Apr 26 22:29:20 CEST 2012


On 04/26/2012 02:24 PM, Sean Carolan wrote:
> In bash you can do this to see if a process is running:
> 
> [scarolan at kurobox:~/bin]$ kill -0 24275
> [scarolan at kurobox:~/bin]$ echo $?
> 0
> 
> Is there a python equivalent?  I tried using os.kill() but did not see
> any way to capture the output.

You might start with something like this ...

# python2.x
try:
    os.kill(24275, 0)
except OSError, e:
    if e.errno != 3: # 3 = No such process
        raise
    # process is not running, do something
else:
    # process is running, do something

It would seem os.kill returns nothing if the signal is sent
successfully, and will raise an exception if not.

HTH!
Marty


More information about the Tutor mailing list