Checking process status from Python

linux, python

Solution

I'd use the `psutil` library:

import psutil

proc = psutil.Process(pid)
if proc.status() == psutil.STATUS_ZOMBIE:
    # Zombie process!

Problem

I am running on a Linux x86-64 system. From a Python (2.6) script, I wish to periodically check whether a given process (identified by pid) has become "defunct"/zombie (this means that entry in the process table exists but the process is doing nothing). It would be also good to know how much CPU the process is consuming (similar to what 'top' command shows). Can somebody give me some pointers on how I can get these in Python?

Original source