从Python检查进程状态
我在Linux x86-64系统上运行。从Python(2.6)脚本中,我希望定期检查给定的进程(由pid标识)是否已成为“失效”/“僵尸”(这意味着进程表中的条目存在,但进程什么也不做)。最好知道进程消耗了多少CPU(类似于“top”命令显示的内容) 有人能给我一些关于如何在Python中获得这些的建议吗 我会使用:从Python检查进程状态,python,linux,Python,Linux,我在Linux x86-64系统上运行。从Python(2.6)脚本中,我希望定期检查给定的进程(由pid标识)是否已成为“失效”/“僵尸”(这意味着进程表中的条目存在,但进程什么也不做)。最好知道进程消耗了多少CPU(类似于“top”命令显示的内容) 有人能给我一些关于如何在Python中获得这些的建议吗 我会使用: 您可以在python中获得如下最佳结果: linux: import sys, os f = os.popen("top -p 1 -n 1", "r") text = f.re
您可以在python中获得如下最佳结果:
linux
:
import sys, os
f = os.popen("top -p 1 -n 1", "r")
text = f.read()
print text
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()
更新
windows
:
import sys, os
f = os.popen("top -p 1 -n 1", "r")
text = f.read()
print text
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()
通过subprocess.Popen()运行
ps
命令。每一行都是一个进程,STAT列中的Z表示你有一个僵尸。ps有大量参数(man ps
),可以为您提供大量信息。假设您使用的是一个有它的操作系统…(哪个OP是)proc.get\u cpu\u times()
,proc.get\u cpu\u percent(interval=1.0)
,以获取cpu使用率。