python定時(shí)檢測(cè)無(wú)響應(yīng)進(jìn)程并重啟的實(shí)例代碼
總有一些程序在windows平臺(tái)表現(xiàn)不穩(wěn)定,動(dòng)不動(dòng)一段時(shí)間就無(wú)響應(yīng),但又不得不用,每次都是發(fā)現(xiàn)問(wèn)題了手動(dòng)重啟,現(xiàn)在寫(xiě)個(gè)腳本定時(shí)檢測(cè)進(jìn)程是否正常,自動(dòng)重啟。
涉及知識(shí)點(diǎn)
- schedule定時(shí)任務(wù)調(diào)度
- os.popen運(yùn)行程序并讀取解析運(yùn)行結(jié)果
代碼分解
腳本主入口
if __name__ == '__main__': #每5秒執(zhí)行檢查任務(wù) schedule.every(5).seconds.do(check_job) #此處固定寫(xiě)法,意思是每秒鐘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)
檢查無(wú)響應(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示例輸出
映像名稱(chēng) PID 會(huì)話(huà)名 會(huì)話(huà)# 內(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í)檢測(cè)無(wú)響應(yīng)進(jìn)程并重啟的實(shí)例代碼 ,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理
- Python爬蟲(chóng)庫(kù)requests獲取響應(yīng)內(nèi)容、響應(yīng)狀態(tài)碼、響應(yīng)頭
- python按鍵按住不放持續(xù)響應(yīng)的實(shí)例代碼
- Python按鈕的響應(yīng)事件詳解
- 關(guān)于python下cv.waitKey無(wú)響應(yīng)的原因及解決方法
- python通過(guò)get,post方式發(fā)送http請(qǐng)求和接收http響應(yīng)的方法
- Python響應(yīng)對(duì)象text屬性亂碼解決方案
相關(guān)文章
Python自動(dòng)化測(cè)試pytest中fixtureAPI簡(jiǎn)單說(shuō)明
這篇文章主要為大家介紹了Python自動(dòng)化測(cè)試pytest中fixtureAPI的簡(jiǎn)單說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10python 出現(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-02Sphinx環(huán)境配置及VScode編寫(xiě)Rst文檔轉(zhuǎn)html的步驟
sphinx主要用于編寫(xiě) reStructuredText 和 Markdown 格式技術(shù)文檔,編寫(xiě)此類(lèi)技術(shù)文檔時(shí)Sphinx工具可將其轉(zhuǎn)為html、pdf、ePub等格式,這篇文章主要介紹了Sphinx環(huán)境配置及VScode編寫(xiě)Rst文檔轉(zhuǎn)html,需要的朋友可以參考下2023-03-03Python用正則表達(dá)式實(shí)現(xiàn)爬取古詩(shī)文網(wǎng)站信息
這篇文章主要給大家介紹了關(guān)于Python如何利用正則表達(dá)式爬取爬取古詩(shī)文網(wǎng)站信息,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Python實(shí)現(xiàn)合并多張圖片成視頻的示例詳解
隨著短視頻的興起,越來(lái)越多的人開(kāi)始用各種形式進(jìn)行視頻制作,本篇博客從程序員的角度為大家解析一下如何通過(guò)?Python?合并多個(gè)圖片為一個(gè)視頻,需要的可以參考一下2023-02-02python中的class_static的@classmethod的巧妙用法
python中的class_static的@classmethod的使用 classmethod的使用,主要針對(duì)的是類(lèi)而不是對(duì)象,在定義類(lèi)的時(shí)候往往會(huì)定義一些靜態(tài)的私有屬性,今天通過(guò)示例代碼看下classmethod的妙用2021-06-06