This is a bit of a kludge, but on *nix you can use os.getpgid(pid) or os.kill(pid, sig) to test the existence of the process ID.
import os
def is_process_running(process_id):
try:
os.kill(process_id, 0)
return True
except OSError:
return False
EDIT: Note that os.kill
works on Windows (as of Python 2.7), while os.getpgid
won't. But the Windows version calls TerminateProcess(), which will "unconditionally cause a process to exit", so I predict that it won't safely return the information you want without actually killing the process if it does exist.
If you're using Windows, please let us know, because none of these solutions are acceptable in that scenario.