日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python监控进程并实现掉线重启

發布時間:2025/3/20 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python监控进程并实现掉线重启 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

分析了具體思路:

1.做個線程定時器,每隔2s執行系統命令查詢指定進程名稱是否存在
2.如果不存在,就重啟;不存在就不進行后續的操作。

相關代碼(以迅雷為例):

import datetime import os import subprocessfrom threading import Timerdef restart_process(process_name):red = subprocess.Popen('tasklist', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)tasklist_str = red.stdout.read().decode(encoding='gbk')re_path = process_name.split("\\")[-1]formattime = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')if re_path not in tasklist_str:# obj = connect_emai()# sendmail('程序卡掉正在重啟。。。', obj)# 發送HTTP請求# url = "http://159.138.131.148/server_offline.html"# request = urllib.request(url)global countcount += 1print(formattime + '第' + str(count) + '次檢測發現異常重連')cmd = process_nameos.system(process_name)# res = subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE,shell=True)# print(res.stderr.read().decode(encoding='gbk'),res.stdout.read().decode(encoding='gbk'))# sendmail('重啟連接成功!',obj)print('yes,connected')else:global error_counterror_count += 1print(formattime + '第' + str(error_count) + '次檢測正在運行中')global timertimer = Timer(2, restart_process, ("start F:\Thunder\Program\Thunder.exe",))timer.start()count = 0 error_count = 0 timer = Timer(2, restart_process, ("start F:\Thunder\Program\ThunderStart.exe",)) timer.start()

如果CPU一直維持在80%以上 就主動殺死進程,并重啟進程,使用了牛逼的psutil 跨系統平臺操作庫。實現代碼

其他遠程啟動等騷操作:https://www.cnblogs.com/my-python-2019/p/11177224.html

總結

以上是生活随笔為你收集整理的python监控进程并实现掉线重启的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。