In Python, without using the / proc file system, how to determine if a given PID works?

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"?

+5
source share
2 answers

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:

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() , sig ​​pid, . Sig , sigaction (2), 0, , . pid.

+11

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, .

+2

All Articles