threadAliveBot.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 updateAccountAvgDaily:
  37. bot(
  38. title="定时任务进程异常挂掉",
  39. detail={
  40. "Job": "updateAccountAvgDaily",
  41. "Time": datetime.datetime.now().__str__()
  42. }
  43. )
  44. if not updateMinigramInfoDaily:
  45. bot(
  46. title="定时任务进程异常挂掉",
  47. detail={
  48. "Job": "updateMinigramInfoDaily",
  49. "Time": datetime.datetime.now().__str__()
  50. }
  51. )
  52. if not updatePublishedMsgDaily:
  53. bot(
  54. title="定时任务进程异常挂掉",
  55. detail={
  56. "Job": "updatePublishedMsgDaily",
  57. "Time": datetime.datetime.now().__str__()
  58. }
  59. )
  60. if not checkVideoStatusDaily:
  61. bot(
  62. title="定时任务进程异常挂掉",
  63. detail={
  64. "Job": "checkVideoStatusDaily",
  65. "Time": datetime.datetime.now().__str__()
  66. }
  67. )
  68. if __name__ == '__main__':
  69. while True:
  70. threadMonitor()
  71. time.sleep(60 * 10)