start.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # coding:utf-8
  2. import argparse
  3. import os
  4. import sys
  5. from argparse import ArgumentParser
  6. from crontab import CronTab
  7. class StartSupervisord:
  8. def __init__(self):
  9. self.cron = CronTab(user='root')
  10. self.python_bin = sys.executable
  11. self.main_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src/yf_supervisord.py')
  12. def init_crond(self):
  13. cmd = "{} {}".format(self.python_bin, self.main_file)
  14. for job in self.cron:
  15. if job.command in cmd:
  16. break
  17. else:
  18. new_job = self.cron.new(cmd, comment="supervise监控脚本,禁止修改")
  19. new_job.minute.every(2)
  20. self.cron.write()
  21. if __name__ == '__main__':
  22. args = ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=''' 云飞后台监控管理启动程序:
  23. 使用方法如下:
  24. python start.py --mode development
  25. ''')
  26. args.add_argument('--mode', type=str, choices=['development', 'production'], required=True, help='''
  27. development 开发模式
  28. production 生产模式
  29. ''')
  30. result = args.parse_args()
  31. mode = result.mode
  32. setting_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config/run_mode')
  33. with open(setting_path, 'w') as fw:
  34. fw.write(mode)
  35. StartSupervisord().init_crond()
  36. print('****** 开启程序成功,两分钟后自动运行supervisord ******')