# coding:utf-8 import argparse import os import sys from argparse import ArgumentParser from crontab import CronTab class StartSupervisord: def __init__(self): self.cron = CronTab(user='root') self.python_bin = sys.executable self.main_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src/yf_supervisord.py') def init_crond(self): cmd = "{} {}".format(self.python_bin, self.main_file) for job in self.cron: if job.command in cmd: break else: new_job = self.cron.new(cmd, comment="supervise监控脚本,禁止修改") new_job.minute.every(2) self.cron.write() if __name__ == '__main__': args = ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=''' 云飞后台监控管理启动程序: 使用方法如下: python start.py --mode development ''') args.add_argument('--mode', type=str, choices=['development', 'production'], required=True, help=''' development 开发模式 production 生产模式 ''') result = args.parse_args() mode = result.mode setting_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config/run_mode') with open(setting_path, 'w') as fw: fw.write(mode) StartSupervisord().init_crond() print('****** 开启程序成功,两分钟后自动运行supervisord ******')