threadAliveBot.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. checkVideoStatusDaily = [line for line in output.splitlines() if 'python3 checkVideoStatusDaily.py' in line]
  28. if not get_off_job:
  29. bot(
  30. title="定时任务进程异常挂掉",
  31. detail={
  32. "Job": "GetOffVideosJob",
  33. "Time": datetime.datetime.now().__str__()
  34. }
  35. )
  36. if not migrate_source_id_job:
  37. bot(
  38. title="定时任务进程异常挂掉",
  39. detail={
  40. "Job": "migrate_source_id_job",
  41. "Time": datetime.datetime.now().__str__()
  42. }
  43. )
  44. if not updateAccountAvgDaily:
  45. bot(
  46. title="定时任务进程异常挂掉",
  47. detail={
  48. "Job": "updateAccountAvgDaily",
  49. "Time": datetime.datetime.now().__str__()
  50. }
  51. )
  52. if not updateMinigramInfoDaily:
  53. bot(
  54. title="定时任务进程异常挂掉",
  55. detail={
  56. "Job": "updateMinigramInfoDaily",
  57. "Time": datetime.datetime.now().__str__()
  58. }
  59. )
  60. if not updatePublishedMsgDaily:
  61. bot(
  62. title="定时任务进程异常挂掉",
  63. detail={
  64. "Job": "updatePublishedMsgDaily",
  65. "Time": datetime.datetime.now().__str__()
  66. }
  67. )
  68. if not checkVideoStatusDaily:
  69. bot(
  70. title="定时任务进程异常挂掉",
  71. detail={
  72. "Job": "checkVideoStatusDaily",
  73. "Time": datetime.datetime.now().__str__()
  74. }
  75. )
  76. if __name__ == '__main__':
  77. while True:
  78. threadMonitor()
  79. time.sleep(60 * 10)