Andreas Thomas Andreas Thomas - 2 years ago 85
Python Question

How do you check in Linux with Python if a process is still running?

The only nice way I've found is:

import sys
import os

try:
os.kill(int(sys.argv[1]), 0)
print "Running"
except:
print "Not running"


(Source)

But is this reliable? Does it work with every process and every distribution?

Answer Source

Mark's answer is the way to go, after all, that's why the /proc file system is there. For something a little more copy/pasteable:

 >>> import os.path
 >>> os.path.exists("/proc/0")
 False
 >>> os.path.exists("/proc/12")
 True
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download