我正在编写一个 python 脚本来保持有错误的程序打开,我需要弄清楚该程序是否没有响应并在 Windows 上将其关闭。我不太清楚该怎么做。
在 Windows 上您可以执行以下操作:
import os
def isresponding(name):
os.system('tasklist /FI "IMAGENAME eq %s" /FI "STATUS eq running" > tmp.txt' % name)
tmp = open('tmp.txt', 'r')
a = tmp.readlines()
tmp.close()
if a[-1].split()[0] == name:
return True
else:
return False
不过,使用 PID 更稳健:
def isrespondingPID(PID):
os.system('tasklist /FI "PID eq %d" /FI "STATUS eq running" > tmp.txt' % PID)
tmp = open('tmp.txt', 'r')
a = tmp.readlines()
tmp.close()
if int(a[-1].split()[1]) == PID:
return True
else:
return False
From tasklist
您可以获得比这更多的信息。要直接获得“NOT RESPONDING”进程,只需在给定的函数中将“running”更改为“notresponding”即可。在这里查看更多信息 http://www.gossamer-threads.com/lists/python/python/796145.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)