最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python監(jiān)控進(jìn)程腳本

 更新時(shí)間:2018年04月12日 08:30:08   作者:左邊的天堂  
這篇文章主要為大家詳細(xì)介紹了python監(jiān)控進(jìn)程腳本,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python監(jiān)控進(jìn)程腳本的具體代碼,供大家參考,具體內(nèi)容如下

原理:

監(jiān)控一個(gè)指定進(jìn)程,每隔5秒鐘獲取其CPU、內(nèi)存使用量超過60%即kill掉該進(jìn)程,獲取其句柄數(shù),超過300也kill掉該進(jìn)程

運(yùn)行環(huán)境是windows 64位系統(tǒng)+python 2.7 64位 ,這里需要使用到psutil 類庫,要另外安裝。腳本里面可以自動(dòng)安裝,前提是你已經(jīng)下載好了安裝包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) 
   
  #啟動(dòng)程序--4種方法 
  #subprocess.Popen(fileName); #非阻塞 
  #subprocess.Popen(fileName).wati(); #阻塞   
  #os.system(fileName); #阻塞 
  #win32api.ShellExecute(0, 'open', fileName, '','',0) 
   
  label = 'Setup' #此處假設(shè)主窗口名為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...") 
 
 
  # 鼠標(biāo)點(diǎn)擊 
  #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) 

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python實(shí)現(xiàn)刪除Android工程中的冗余字符串

    Python實(shí)現(xiàn)刪除Android工程中的冗余字符串

    這篇文章主要介紹了Python實(shí)現(xiàn)刪除Android工程中的冗余字符串,本文實(shí)現(xiàn)的是刪除Android資源(語言)國際化機(jī)制中的一些冗余字符串,需要的朋友可以參考下
    2015-01-01
  • pandas創(chuàng)建DataFrame的7種方法小結(jié)

    pandas創(chuàng)建DataFrame的7種方法小結(jié)

    這篇文章主要介紹了pandas創(chuàng)建DataFrame的7種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • 解析python 中/ 和 % 和 //(地板除)

    解析python 中/ 和 % 和 //(地板除)

    這篇文章主要介紹了python 中/ 和 % 和 //(地板除)的區(qū)別及簡介,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-06-06
  • Pytorch maxpool的ceil_mode用法

    Pytorch maxpool的ceil_mode用法

    今天小編就為大家分享一篇Pytorch maxpool的ceil_mode用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-02-02
  • Python全棧之列表數(shù)據(jù)類型詳解

    Python全棧之列表數(shù)據(jù)類型詳解

    這篇文章主要給大家介紹了關(guān)于Python全棧之列表數(shù)據(jù)類型的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • python怎么提高計(jì)算速度

    python怎么提高計(jì)算速度

    在本篇文章里小編給大家分享的是一篇關(guān)于python中如何提高計(jì)算速度的技術(shù)文章,需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python Dataframe字符串合并的操作方法

    python Dataframe字符串合并的操作方法

    Dataframe的字符串合并包括2種場景,1.合并df中其中幾列字符串;2.將df中的字符串與外部字符串合并,本文主要介紹在Python下對(duì)Dataframe進(jìn)行字符串合并操作的方法,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • python/golang實(shí)現(xiàn)循環(huán)鏈表的示例代碼

    python/golang實(shí)現(xiàn)循環(huán)鏈表的示例代碼

    這篇文章主要介紹了python/golang如何實(shí)現(xiàn)循環(huán)鏈表,幫助大家更好的理解和學(xué)習(xí)循環(huán)鏈表的實(shí)現(xiàn)方法,感興趣的朋友可以了解下
    2020-09-09
  • Pyinstaller打包工具的使用以及避坑

    Pyinstaller打包工具的使用以及避坑

    本文主要的是pyinstaller在windows下的基本使用和基礎(chǔ)避坑,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 圖片去摩爾紋簡述實(shí)現(xiàn)python代碼示例

    圖片去摩爾紋簡述實(shí)現(xiàn)python代碼示例

    這篇文章主要為大家介紹了圖片去摩爾紋簡述實(shí)現(xiàn)的python代碼示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02

最新評(píng)論

五大连池市| 昆明市| 江口县| 孟州市| 社旗县| 富源县| 柯坪县| 长岛县| 顺平县| 孟津县| 平武县| 昌都县| 孝义市| 东丰县| 宁远县| 望谟县| 寿宁县| 筠连县| 云南省| 丹阳市| 邓州市| 诏安县| 和硕县| 蓝山县| 徐水县| 民勤县| 怀仁县| 甘肃省| 饶平县| 巴彦县| 石狮市| 厦门市| 灵丘县| 奉新县| 凯里市| 大名县| 百色市| 苍南县| 古浪县| 双峰县| 玉田县|