123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- # -*- codeing = utf-8 -*-
- # @Time : 2023/11/29 16:00
- # @Author : Clown
- # @File : demo_check_error_pid.py
- # @Software : PyCharm
- import subprocess
- import requests
- import time
- from datetime import datetime, timedelta
- def sendMsgToRot(url, msg_txt):
- params_json = {
- "msgtype": "markdown",
- "markdown": {
- "content": msg_txt,
- "mentioned_list": ["@all"]
- }}
- resp = requests.post (url, json=params_json).text
- print (resp)
- def check_process(process_name):
- try:
- # 执行 ps -aux 命令并获取输出
- output = subprocess.check_output(['ps', '-aux'])
- pids = output.decode().split('\n')
- for i in pids:
- if process_name in i:
- pid = i
- return pid
- except subprocess.CalledProcessError:
- return []
- def main(process_name):
- # process_name = "flask" # 替换为你要监控的进程名称
- pids = check_process(process_name)
- if len(pids) > 0:
- print(f"进程 {process_name} 正在运行,进程ID为:{pids}")
- else:
- command = f"nohup python /home/python_flies/{process_name} &"
- subprocess.Popen(command, shell = True)
- url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cf7139e1-e623-41ca-8541-5dc6e26d43b0'
- msg_txt = f'进程 {process_name} 未运行,已重新启动运行'
- sendMsgToRot(url, msg_txt)
- print(f"进程 {process_name} 未运行")
- def run():
- # 初始化时间周期
- # 更新间隔
- minute = 10
- f_now = datetime.now ()
- f_mm = f_now.strftime ("%M")
- f_ss = f_now.strftime ("%S")
- f_c = int (f_mm) % minute
- wait_s = (minute - f_c - 1) * 60 + 59 - int (f_ss)
- time.sleep (wait_s)
- # 执行循环
- while True:
- now = datetime.now ()
- mm = now.strftime ("%M")
- ss = now.strftime ("%S")
- c = int (mm) % minute
- if c == 0 and ss == '01':
- s = time.time ()
- process_names = ['demo_OceanEngineAd.py', 'demo_baiduAd.py']
- for process_name in process_names:
- main(process_name)
- time.sleep (minute * 60 - 0.5 - (time.time () - s))
- if __name__ == "__main__":
- run()
|