windows进程监控脚本
Posted shiji888
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了windows进程监控脚本相关的知识,希望对你有一定的参考价值。
有一些老的业务在windows服务器iis站点上,部分会跑满cpu,用了个python脚本来监控进程,当cpu过高时杀死进程,同时记录日志
import psutil import os import time import logging def KillProcess(): os.kill(pid,2) def ProcessLog(): LOG_FORMAT = "%(asctime)s %(name)s %(levelname)s %(pathname)s %(message)s " DATE_FORMAT = ‘%Y-%m-%d %H:%M:%S %a ‘ logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT, filename=r"D:SoftpythonjiaobenProcess.log" ) logging.info(‘%s The end of this process has been!!!‘,FilePath) ##################################################################################################### if __name__ == ‘__main__‘: pl=psutil.pids() r=[] for pid in pl: try: p=psutil.Process(pid) r.append((pid,p.name(),p.cpu_percent())) FilePath=p.username() CpuPercent=p.cpu_percent(interval=0.2) if (p.name() == ‘w3wp.exe‘) and (CpuPercent >= 400): KillProcess() ProcessLog() except: pass
以上是关于windows进程监控脚本的主要内容,如果未能解决你的问题,请参考以下文章