Checking process status with Python

I work on Linux x86-64. From a Python (2.6) script, I want to periodically check if a given process (identified by pid) has become "non-existent" / zombie (this means that the entry in the process table exists, but the process does nothing). It would also be useful to know how much the processor consumes this process (similar to what the "top" command shows).

Can someone give me some pointers on how I can get them in Python?

+4
source share
2 answers

I would use psutil library :

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

you can get the best result in python as below:

linux :

 import sys, os f = os.popen("top -p 1 -n 1", "r") text = f.read() print text 

update

windows :

 from os import popen from sys import stdin ps = popen("C:/WINDOWS/system32/tasklist.exe","r") pp = ps.readlines() ps.close() # wow, look at the robust parser! pp.pop(0) # blank line ph = pp.pop(0) # header line pp.pop(0) # === print ("%d processes reported." % len(pp)) print ("First process in list:") print (pp[0]) stdin.readline() 
+3
source

All Articles