threadAliveBot.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. """
  2. @author: luojunhui
  3. @description: 监测进程是否 alive in every 10 minutes
  4. """
  5. import datetime
  6. import subprocess
  7. import time
  8. from applications import bot
  9. def threadMonitor():
  10. """
  11. 校验进程是否 alive
  12. :return:
  13. """
  14. result = subprocess.run(
  15. ["ps", "aux"],
  16. stdout=subprocess.PIPE,
  17. text=True
  18. )
  19. # 获取命令输出结果
  20. output = result.stdout
  21. # filter
  22. get_off_job = [line for line in output.splitlines() if 'python3 getOffVideosDaily.py' in line]
  23. migrate_source_id_job = [line for line in output.splitlines() if 'python3 migrateRootSourceId.py' in line]
  24. updateAccountAvgDaily = [line for line in output.splitlines() if 'python3 updateAccountAvgDaily.py' in line]
  25. updateMinigramInfoDaily = [line for line in output.splitlines() if 'python3 updateMinigramInfoDaily.py' in line]
  26. updatePublishedMsgDaily = [line for line in output.splitlines() if 'python3 updatePublishedMsgDaily.py' in line]
  27. if not get_off_job:
  28. bot(
  29. title="定时任务进程异常挂掉",
  30. detail={
  31. "Job": "GetOffVideosJob",
  32. "Time": datetime.datetime.now().__str__()
  33. }
  34. )
  35. if not migrate_source_id_job:
  36. bot(
  37. title="定时任务进程异常挂掉",
  38. detail={
  39. "Job": "migrate_source_id_job",
  40. "Time": datetime.datetime.now().__str__()
  41. }
  42. )
  43. if not updateAccountAvgDaily:
  44. bot(
  45. title="定时任务进程异常挂掉",
  46. detail={
  47. "Job": "updateAccountAvgDaily",
  48. "Time": datetime.datetime.now().__str__()
  49. }
  50. )
  51. if not updateMinigramInfoDaily:
  52. bot(
  53. title="定时任务进程异常挂掉",
  54. detail={
  55. "Job": "updateMinigramInfoDaily",
  56. "Time": datetime.datetime.now().__str__()
  57. }
  58. )
  59. if not updatePublishedMsgDaily:
  60. bot(
  61. title="定时任务进程异常挂掉",
  62. detail={
  63. "Job": "updatePublishedMsgDaily",
  64. "Time": datetime.datetime.now().__str__()
  65. }
  66. )
  67. if __name__ == '__main__':
  68. while True:
  69. threadMonitor()
  70. time.sleep(60 * 10)