python通过什么来判断当前程序是否在,如何判断进程是否在Windows上的Python中响应...

I am writing a python script to keep a buggy program open and I need to figure out if the program is not respoding and close it on windows. I can't quite figure out how to do this.

解决方案

On Windows you can do this:

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

It is more robust to use the PID though:

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 you can get more information than that. To get the "NOT RESPONDING" processes directly, just change "running" by "not responding" in the functions given. See more info here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值