我在 Linux x86-64 系统上运行。从 Python (2.6) 脚本中,我希望定期检查给定进程(由 pid 标识)是否已变为“已失效”/僵尸(这意味着进程表中的条目存在但进程什么也不做)。知道进程消耗了多少 CPU 也是很好的(类似于“top”命令显示的内容)。
有人可以给我一些关于如何在 Python 中获得这些的指示吗?
我会使用psutil
图书馆:
import psutil
proc = psutil.Process(pid)
if proc.status() == psutil.STATUS_ZOMBIE:
# Zombie process!
您可以在 python 中获得最佳结果,如下所示:
linux
:
import sys, os
f = os.popen("top -p 1 -n 1", "r")
text = f.read()
print text
更新
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()