亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python定時(shí)檢測無響應(yīng)進(jìn)程并重啟的實(shí)例代碼

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

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

涉及知識點(diǎn)

  1. schedule定時(shí)任務(wù)調(diào)度
  2. os.popen運(yùn)行程序并讀取解析運(yùn)行結(jié)果

代碼分解

腳本主入口

if __name__ == '__main__':
  #每5秒執(zhí)行檢查任務(wù)
  schedule.every(5).seconds.do(check_job)
  #此處固定寫法,意思是每秒鐘schedule看下是否有pending的任務(wù),有就執(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)
#每小時(shí)
schedule.every().hour.do(job, message='things')
#每天10點(diǎn)30分
schedule.every().day.at("10:30").do(job)
while True:
  schedule.run_pending()
  time.sleep(1)

檢查無響應(yīng)進(jìn)程并重啟

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')
}

查找符合條件的進(jìn)程列表

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í)行結(jié)果

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 會話名       會話#    內(nèi)存使用
========================= ======== ================ =========== ============
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)

總結(jié)

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

相關(guān)文章

  • Python讀取csv文件實(shí)例解析

    Python讀取csv文件實(shí)例解析

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

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

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

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

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

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

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

    解決hive中導(dǎo)入text文件遇到的坑

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

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

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

    Python用正則表達(dá)式實(shí)現(xiàn)爬取古詩文網(wǎng)站信息

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

    python增加矩陣維度的實(shí)例講解

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

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

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

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

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

最新評論