windows进程监控脚本
2021-03-10 22:35
标签:interval exe this pytho username end port his roc 有一些老的业务在windows服务器iis站点上,部分会跑满cpu,用了个python脚本来监控进程,当cpu过高时杀死进程,同时记录日志 windows进程监控脚本 标签:interval exe this pytho username end port his roc 原文地址:https://www.cnblogs.com/shiji888/p/12668480.htmlimport 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:\Soft\python\jiaoben\Process.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