Oct*_*age 6 python operating-system process
我正在编写一个 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
Run Code Online (Sandbox Code Playgroud)
不过,使用 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
Run Code Online (Sandbox Code Playgroud)
从中tasklist您可以获得比这更多的信息。要直接获得“NOT RESPONDING”进程,只需在给定的函数中将“running”更改为“notresponding”即可。请在此处查看更多信息。
| 归档时间: |
|
| 查看次数: |
4046 次 |
| 最近记录: |