幽灵资源网 Design By www.bzswh.com
本文实例为大家分享了python监控进程脚本的具体代码,供大家参考,具体内容如下
原理:
监控一个指定进程,每隔5秒钟获取其CPU、内存使用量超过60%即kill掉该进程,获取其句柄数,超过300也kill掉该进程
运行环境是windows 64位系统+python 2.7 64位 ,这里需要使用到psutil 类库,要另外安装。脚本里面可以自动安装,前提是你已经下载好了安装包psutil-3.3.0.win-amd64-py2.7.exe
下面看代码:
#!/usr/bin/env python # -*- coding:utf-8 -*- import time from datetime import date, datetime, timedelta import platform import os import win32ui,win32api,win32con,win32gui import subprocess def install(): print("install psutil...") sysstr = platform.system() if(sysstr =="Windows"): print ("Call Windows tasks") bit,type=platform.architecture() print ("os bit: %s " % bit) #print ("os type: %s " % type) if(bit == "64bit"): fileName="psutil-3.3.0.win-amd64-py2.7.exe"; else: fileName="psutil-3.3.0.win32-py2.7.exe"; print("will install the file [%s]" % fileName) #启动程序--4种方法 #subprocess.Popen(fileName); #非阻塞 #subprocess.Popen(fileName).wati(); #阻塞 #os.system(fileName); #阻塞 #win32api.ShellExecute(0, 'open', fileName, '','',0) label = 'Setup' #此处假设主窗口名为tt hld = win32gui.FindWindow(None, label) count=0 while (hld == 0 and count<20): print("the setup is no running,will run it...") count += 1 win32api.ShellExecute(0, 'open', fileName, '','',0) print("sleep 1 seconds...") time.sleep(0.5) #wnd = win32ui.GetForegroundWindow() #print wnd.GetWindowText() hld = win32gui.FindWindow(None, label) print("hld is %s" % hld) pwin=win32ui.FindWindow(None,label) print("pwin is %s" % pwin) print pwin.GetWindowText() print("click...") button2=win32ui.FindWindowEx(pwin,None,None,'下一步(&N) >') #找到按钮 button2.SendMessage(win32con.BM_CLICK, 0,-1) button2=win32ui.FindWindowEx(pwin,None,None,'下一步(&N) >') #找到按钮 button2.SendMessage(win32con.BM_CLICK, 0,-1) button2=win32ui.FindWindowEx(pwin,None,None,'下一步(&N) >') #找到按钮 button2.SendMessage(win32con.BM_CLICK, 0,-1) button2=win32ui.FindWindowEx(pwin,None,None,'完成') #找到按钮 button2.SendMessage(win32con.BM_CLICK, 0,-1) print("install done...") # 鼠标点击 #print("click...") #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) #time.sleep(0.1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) #time.sleep(1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) #time.sleep(0.1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) #time.sleep(1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) #time.sleep(0.1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) #time.sleep(1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0) #time.sleep(0.1) #win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0) elif(sysstr == "Linux"): print ("Call Linux tasks") else: print ("Other System tasks") try: print("import psutil...") import psutil except Exception,e: print Exception,":",e install() import psutil def get_proc_by_id(pid): return psutil.Process(pid) def get_proc_by_name(pname): """ get process by name return the first process if there are more than one """ for proc in psutil.process_iter(): try: <span style="white-space:pre"> </span># return if found one if proc.name().lower() == pname.lower():<span style="white-space:pre"> </span> return proc<span style="white-space:pre"> </span> except psutil.AccessDenied: pass except psutil.NoSuchProcess: pass return None def getProcess(pname, day=0, hour=0, min=0, second=0): # Init time now = datetime.now() strnow = now.strftime('%Y-%m-%d %H:%M:%S') print "now:",strnow # First next run time period = timedelta(days=day, hours=hour, minutes=min, seconds=second) next_time = now + period strnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S') print "next run time:",strnext_time while True: # Get system current time iter_now = datetime.now() iter_now_time = iter_now.strftime('%Y-%m-%d %H:%M:%S') if str(iter_now_time) == str(strnext_time): next_time = iter_now + period strnext_time = next_time.strftime('%Y-%m-%d %H:%M:%S') print "next run time:",strnext_time try: Process=get_proc_by_name(pname) except Exception,e: print Exception,":",e if Process != None : print "-------Found the process : %s" % Process.name(); print("pid is (%s)" % Process.pid); Cpu_usage = Process.cpu_percent(interval=1) print("cpu percent is (%s)" % Cpu_usage); if (100-Cpu_usage) < 0.1 : print "cpu percent larger 60,now will terminate this process !"; Process.terminate(); Process.wait(timeout=3); continue RAM_percent = Process.memory_percent() print("memory percent is (%s)" % RAM_percent); if (60-RAM_percent) < 0.1 : print "memory percent larger 60,now will terminate this process !"; Process.terminate(); Process.wait(timeout=3); continue all_files = list(Process.open_files()); print("open files size is (%d)" % len(all_files)); if (len(all_files)>300) : print "open files size larger 300,now will terminate this process !"; Process.terminate(); Process.wait(timeout=3); continue Threads_Num=Process.num_threads() print("threads number is (%s)" % Threads_Num); if (Threads_Num>200) : print "threads number larger 200,now will terminate this process !"; Process.terminate(); Process.wait(timeout=3); continue else : print "-------No found the process : %s" % pname; continue if __name__ == '__main__': print("main....") getProcess("QQ.exe",second=5)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,监控进程
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。