10

If I known a process's pid, how can I tell if the process is a zombie using Python ?

crizCraig
  • 7,808
  • 5
  • 52
  • 52
Bdfy
  • 21,109
  • 53
  • 126
  • 178

2 Answers2

15

You could use a the status feature from psutil:

import psutil
p = psutil.Process(the_pid_you_want)
if p.status == psutil.STATUS_ZOMBIE:
    ....
bruno desthuilliers
  • 72,252
  • 6
  • 79
  • 103
Mat
  • 195,986
  • 40
  • 382
  • 396
14

here's a quick hack using procfs (assuming you're using Linux):

def procStatus(pid):
    for line in open("/proc/%d/status" % pid).readlines():
        if line.startswith("State:"):
            return line.split(":",1)[1].strip().split(' ')[0]
    return None

this function should return 'Z' for zombies.

Andre Holzner
  • 17,661
  • 6
  • 53
  • 61