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

這篇文章主要介紹了python定時檢測無響應進程並重啓的實例代碼,非常不錯,具有一定的參考借鑑價值,需要的朋友可以參考下

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

涉及知識點

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

代碼分解

腳本主入口

if __name__ == '__main__':
  #每5秒執行檢查任務
  schedule.every(5).seconds.do(check_job)
  #此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務,有就執行
  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)

解析命令執行結果

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定時檢測無響應進程並重啓的實例代碼 ,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請註明出處,謝謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章