码迷,mamicode.com
首页 > Windows程序 > 详细

windows进程监控脚本

时间:2020-04-09 18:00:53      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:interval   exe   this   pytho   username   end   port   his   roc   

有一些老的业务在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:\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

 

windows进程监控脚本

标签:interval   exe   this   pytho   username   end   port   his   roc   

原文地址:https://www.cnblogs.com/shiji888/p/12668480.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!