11

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

crizCraig
  • 8,487
  • 6
  • 54
  • 53
Bdfy
  • 23,141
  • 55
  • 131
  • 179

2 Answers2

17

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
  • 75,974
  • 6
  • 88
  • 118
Mat
  • 202,337
  • 40
  • 393
  • 406
15

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
  • 18,333
  • 6
  • 54
  • 63