Let's say I have a PID, for example 555. I want to see if this pid is working or has completed. I can check / proc / but I do not have access to this in my production environment. What is the best way to do this, with the exception of something hacked, like opening a pipe to "ps"?
Use the function os.kill()with signal number 0. If the pid process exists, the call will succeed, otherwise it will raise an exception OSError:
os.kill()
OSError
try: os.kill(pid, 0) print("process exists") except OSError: print("process does not exist")
The documentation for kill(2)on my system reads:
kill(2)
kill() , sig pid, . Sig , sigaction (2), 0, , . pid.
os.kill(), , , kill , , . - , - . , :
try: os.kill(pid, 0) print 'Process exists and we can kill it' except OSError, e: if e.errno == 1: print 'Process exists, but we cannot kill it' else: raise
, , , , , root , , UID, .