以python脚本创建“守护”进程

 

监控某进程的状态,并在进程异常退出时尝试重启并发送邮件通知。

以python脚本创建“守护”进程

邮件通知需要用到 smtplib 库,需要提前安装。

监控进程需要用到 subprocess 库。

import time
import subprocess
import smtplib
from email.mime.text import MIMEText
from email.header import Header

def send_email(subject, body):
    # 请根据实际情况配置邮件服务器和端口
    smtp_server = 'your_smtp_server'
    smtp_port = 587
    # 例如QQ邮箱:
    smtp_server = 'smtp.qq.com'
    smtp_port = 465
    # 飞书邮箱请参看:
    # https://www.feishu.cn/hc/zh-CN/articles/902478147400-%E5%9C%A8%E7%AC%AC%E4%B8%89%E6%96%B9%E9%82%AE%E7%AE%B1%E5%AE%A2%E6%88%B7%E7%AB%AF%E7%99%BB%E5%BD%95%E9%A3%9E%E4%B9%A6%E9%82%AE%E7%AE%B1

    # 请替换为你的邮箱地址和授权码
    sender_email = 'your_email@example.com'
    sender_password = 'your_authorization_code'

    # 请替换为接收通知的邮箱地址,可以发给自己
    receiver_email = 'receiver_email@example.com'

    message = MIMEText(body, 'plain', 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')
    message['From'] = sender_email
    message['To'] = receiver_email

    try:
        server=smtplib.SMTP_SSL(smtp_server, smtp_port)  # 发件人邮箱中的SMTP服务器,端口是25
        server.login(sender_email, sender_password)  # 括号中对应的是发件人邮箱账号、邮箱密码
        server.sendmail(sender_email,[receiver_email,],message.as_string())  # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件
        server.quit()  # 关闭连接
        print("邮件发送成功")

    except Exception as e:
        print("发送邮件时出现错误:", str(e))

def handle_crash(restart_attempts, restart_interval):
    # 在子程序崩溃时执行相应的处理代码
    subject = "进程崩溃通知"
    body = "进程崩溃了!"

    # 发送邮件通知
    send_email(subject, body)

    if restart_attempts > 0:
        # 重启子程序
        print("尝试重启子程序...")
        restart_attempts -= 1

        try:
            process = subprocess.Popen(["your_program", "arg1", "arg2", ...], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            process.wait()

            if process.returncode != 0:
                # 子程序再次崩溃,延迟一段时间后继续重启
                print("子程序再次崩溃,等待重启...")
                time.sleep(restart_interval)
                handle_crash(restart_attempts, restart_interval)

        except subprocess.CalledProcessError:
            # 子程序启动失败
            print("子程序启动失败!")

    else:
        print("达到重启次数限制,无法继续重启子程序。")

def main():
  try:
      # 启动子程序
      process = subprocess.Popen(["your_program", "arg1", "arg2", ...], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

      # 阻塞父进程,等待子进程执行完毕
      process.wait()

      if process.returncode != 0:
          # 子程序崩溃
          restart_attempts = 3  # 重启次数限制
          restart_interval = 10  # 重启间隔时间(秒)
          handle_crash(restart_attempts, restart_interval)

  except subprocess.CalledProcessError:
      # 子程序启动失败
      print("子程序启动失败!")

if __name__ == "__main__":
    main()

参考资料

  1. Python SMTP发送邮件