monitor.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import psutil
  3. from apscheduler.schedulers.blocking import BlockingScheduler
  4. def monitor(_exe,_file,args=[]):
  5. _flag = False
  6. if _file is not None and _file!="":
  7. if os.path.exists(_file):
  8. _file = os.path.abspath(_file)
  9. else:
  10. print("file not exists")
  11. for _p in psutil.process_iter():
  12. try:
  13. proc_info = _p.as_dict(attrs={"pid","name","exe","cmdline"})
  14. _cmdline = proc_info.get("cmdline",[])
  15. if len(_cmdline)>=2:
  16. _exe_file = _cmdline[1]
  17. if os.path.exists(_exe_file):
  18. if os.path.abspath(_exe_file)==_file:
  19. _flag = True
  20. print("process %s %s is processing"%(_exe,_file))
  21. except Exception as e:
  22. print(str(e))
  23. if not _flag:
  24. os.system("nohup %s %s %s &"%(_exe,_file," ".join(args)))
  25. def monitor_processes():
  26. list_process = [{"exe":"/root/ENV/bin/python",
  27. "file":"/root/BaseDataMaintenance/start_sychro_attachFix.py"}]
  28. for _process in list_process:
  29. monitor(_process.get("exe",""),_process.get("file",""),[])
  30. def start_monitor():
  31. schedule = BlockingScheduler()
  32. schedule.add_job(monitor_processes,"cron",minute="*/1")
  33. schedule.start()
  34. if __name__=="__main__":
  35. start_monitor()