欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python定時檢測無響應進程并重啟的實例代碼

 更新時間:2019年04月22日 08:56:28   作者:零壹視界  
這篇文章主要介紹了python定時檢測無響應進程并重啟的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

總有一些程序在windows平臺表現(xiàn)不穩(wěn)定,動不動一段時間就無響應,但又不得不用,每次都是發(fā)現(xiàn)問題了手動重啟,現(xiàn)在寫個腳本定時檢測進程是否正常,自動重啟。

涉及知識點

  1. schedule定時任務調度
  2. os.popen運行程序并讀取解析運行結果

代碼分解

腳本主入口

if __name__ == '__main__':
  #每5秒執(zhí)行檢查任務
  schedule.every(5).seconds.do(check_job)
  #此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務,有就執(zhí)行
  while True:
    schedule.run_pending()
    time.sleep(1)

schedule的其它示例

import schedule
import time
def job(message='stuff'):
  print("I'm working on:", message)
#每10分鐘
schedule.every(10).minutes.do(job)
#每小時
schedule.every().hour.do(job, message='things')
#每天10點30分
schedule.every().day.at("10:30").do(job)
while True:
  schedule.run_pending()
  time.sleep(1)

檢查無響應進程并重啟

def check_job():
  process_name = "xx.exe"
  not_respond_list = list_not_response(process_name)
  if len(not_respond_list) <= 0:
    return
  pid_params = " ".join(["/PID " + pid for pid in not_respond_list])
  os.popen("taskkill /F " + pid_params)
  if len(list_process(process_name)) <= 0:
    start_program(r'E:\xx\xx.exe')
}

查找符合條件的進程列表

def list_process(process_name, not_respond=False):
  cmd = 'tasklist /FI "IMAGENAME eq %s"'
  if not_respond:
    cmd = cmd + ' /FI "STATUS eq Not Responding"'
  output = os.popen(cmd % process_name)
  return parse_output(output.read())
def list_not_response(process_name):
  return list_process(process_name, True)

解析命令執(zhí)行結果

def parse_output(output):
  print(output)
  pid_list = []
  lines = output.strip().split("\n")
  if len(lines) > 2:
    for line in lines[2:]:
      pid_list.append(line.split()[1])
  return pid_list

tasklist示例輸出

映像名稱            PID 會話名       會話#    內存使用
========================= ======== ================ =========== ============
WizChromeProcess.exe     1620 Console          1   32,572 K

完整代碼

import os
import time
import schedule
def parse_output(output):
  print(output)
  pid_list = []
  lines = output.strip().split("\n")
  if len(lines) > 2:
    for line in lines[2:]:
      pid_list.append(line.split()[1])
  return pid_list
def list_not_response(process_name):
  return list_process(process_name, True)
def list_process(process_name, not_respond=False):
  cmd = 'tasklist /FI "IMAGENAME eq %s"'
  if not_respond:
    cmd = cmd + ' /FI "STATUS eq Not Responding"'
  output = os.popen(cmd % process_name)
  return parse_output(output.read())
def start_program(program):
  os.popen(program)
def check_job():
  process_name = "xx.exe"
  not_respond_list = list_not_response(process_name)
  if len(not_respond_list) <= 0:
    return
  pid_params = " ".join(["/PID " + pid for pid in not_respond_list])
  os.popen("taskkill /F " + pid_params)
  if len(list_process(process_name)) <= 0:
    start_program(r'E:\xxx\xx.exe')
if __name__ == '__main__':
  schedule.every(5).seconds.do(check_job)
  while True:
    schedule.run_pending()
    time.sleep(1)

總結

以上所述是小編給大家介紹的python定時檢測無響應進程并重啟的實例代碼 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!

相關文章

  • Python讀取csv文件實例解析

    Python讀取csv文件實例解析

    這篇文章主要介紹了Python讀取csv文件實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • Python自動化測試pytest中fixtureAPI簡單說明

    Python自動化測試pytest中fixtureAPI簡單說明

    這篇文章主要為大家介紹了Python自動化測試pytest中fixtureAPI的簡單說明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-10-10
  • python 出現(xiàn)SyntaxError: non-keyword arg after keyword arg錯誤解決辦法

    python 出現(xiàn)SyntaxError: non-keyword arg after keyword arg錯誤解決辦

    這篇文章主要介紹了python 出現(xiàn)SyntaxError: non-keyword arg after keyword arg錯誤解決辦法的相關資料,需要的朋友可以參考下
    2017-02-02
  • 詳解pandas df.iloc[]的典型用法

    詳解pandas df.iloc[]的典型用法

    本文主要介紹了詳解pandas df.iloc[]的典型用法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-08-08
  • 解決hive中導入text文件遇到的坑

    解決hive中導入text文件遇到的坑

    這篇文章主要介紹了解決hive中導入text文件遇到的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • Sphinx環(huán)境配置及VScode編寫Rst文檔轉html的步驟

    Sphinx環(huán)境配置及VScode編寫Rst文檔轉html的步驟

    sphinx主要用于編寫 reStructuredText 和 Markdown 格式技術文檔,編寫此類技術文檔時Sphinx工具可將其轉為html、pdf、ePub等格式,這篇文章主要介紹了Sphinx環(huán)境配置及VScode編寫Rst文檔轉html,需要的朋友可以參考下
    2023-03-03
  • Python用正則表達式實現(xiàn)爬取古詩文網站信息

    Python用正則表達式實現(xiàn)爬取古詩文網站信息

    這篇文章主要給大家介紹了關于Python如何利用正則表達式爬取爬取古詩文網站信息,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • python增加矩陣維度的實例講解

    python增加矩陣維度的實例講解

    下面小編就為大家分享一篇python增加矩陣維度的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python實現(xiàn)合并多張圖片成視頻的示例詳解

    Python實現(xiàn)合并多張圖片成視頻的示例詳解

    隨著短視頻的興起,越來越多的人開始用各種形式進行視頻制作,本篇博客從程序員的角度為大家解析一下如何通過?Python?合并多個圖片為一個視頻,需要的可以參考一下
    2023-02-02
  • python中的class_static的@classmethod的巧妙用法

    python中的class_static的@classmethod的巧妙用法

    python中的class_static的@classmethod的使用 classmethod的使用,主要針對的是類而不是對象,在定義類的時候往往會定義一些靜態(tài)的私有屬性,今天通過示例代碼看下classmethod的妙用
    2021-06-06

最新評論