monitor_process2.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import logging
  2. import os
  3. import re
  4. import time
  5. import psutil
  6. convert_port_list = ["15010"]
  7. # ocr_port_list = ["15011", "15013", "15015"]
  8. # ocr_port_list = ["15011", "15013", "15015", "15017", "15019"]
  9. # otr_port_list = ["15012", "15014", "15016", "15018", "15020"]
  10. ocr_port_list = ["15011", "15013", "15015", "15017", "15019", "15021"]
  11. otr_port_list = ["15012", "15014", "15016", "15018", "15020", "15022"]
  12. soffice_port_list = ["16000", "16001", "16002", "16003", "16004", "16005",
  13. "16006", "16007", "16008", "16009"]
  14. python_path = "/root/miniconda3/bin/python"
  15. interface_path = "/data/format_conversion_maxcompute"
  16. std_out = " >>/convert.out 2>&1 &"
  17. std_out_gpu = " >>/gpu.out 2>&1 &"
  18. convert_comm = "nohup " + python_path + " " + interface_path + "/format_convert/convert.py #" + std_out
  19. ocr_comm = "nohup " + python_path + " " + interface_path + "/ocr/ocr_interface.py # 0" + std_out_gpu
  20. otr_comm = "nohup " + python_path + " " + interface_path + "/otr/otr_interface.py # 0" + std_out_gpu
  21. soffice_comm = "docker run --init -itd --log-opt max-size=10m --log-opt max-file=3 -p #:16000 soffice:v2 bash"
  22. def get_port():
  23. net_conn = psutil.net_connections()
  24. current_port_list = []
  25. for conn in net_conn:
  26. current_port_list.append(str(conn.laddr.port))
  27. current_port_list = list(set(current_port_list))
  28. current_port_list.sort(key=lambda x: x)
  29. # print(current_port_list)
  30. return current_port_list
  31. def restart(process_type, port):
  32. if process_type == "convert":
  33. _comm = re.sub("#", port, convert_comm)
  34. elif process_type == "ocr":
  35. _comm = re.sub("#", port, ocr_comm)
  36. elif process_type == "otr":
  37. _comm = re.sub("#", port, otr_comm)
  38. elif process_type == "soffice":
  39. _comm = re.sub("#", port, soffice_comm)
  40. else:
  41. _comm = "netstat -nltp"
  42. print("no process_type", process_type)
  43. # os.system("netstat -nltp")
  44. os.system("echo $(date +%F%n%T)")
  45. print("restart comm", _comm)
  46. os.system(_comm)
  47. def kill_soffice(limit_sec=20):
  48. pid_list = psutil.pids()
  49. for pid in pid_list:
  50. process = psutil.Process(pid)
  51. process_cmd = ''
  52. for c in process.cmdline():
  53. process_cmd += c + " "
  54. if process_cmd.strip() == "":
  55. continue
  56. if process.status() == "zombie":
  57. print("zombie cmd", process_cmd)
  58. if re.search("soffice", process.exe()):
  59. if process.status() == "zombie":
  60. ppid = process.ppid
  61. comm = "kill -9 " + str(ppid)
  62. print("kill defunct process ", str(ppid), str(process.exe()))
  63. os.system("echo $(date +%F%n%T)")
  64. os.system(comm)
  65. start_time = process.create_time()
  66. now_time = time.time()
  67. run_time = now_time-start_time
  68. if run_time >= limit_sec:
  69. comm = "kill -9 " + str(pid)
  70. print("kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  71. os.system("echo $(date +%F%n%T)")
  72. os.system(comm)
  73. def kill_defunct():
  74. pid_list = psutil.pids()
  75. for pid in pid_list:
  76. process = psutil.Process(pid)
  77. if process.status() == "zombie":
  78. ppid = process.ppid
  79. process = psutil.Process(ppid)
  80. process.kill()
  81. process.send_signal(9)
  82. break
  83. # comm = "kill -9 " + str(ppid)
  84. # print("kill process ", str(ppid))
  85. # os.system("echo $(date +%F%n%T)")
  86. # os.system(comm)
  87. def monitor():
  88. current_port_list = get_port()
  89. for p in convert_port_list:
  90. if p not in current_port_list:
  91. restart("convert", p)
  92. for p in ocr_port_list:
  93. if p not in current_port_list:
  94. restart("ocr", p)
  95. for p in otr_port_list:
  96. if p not in current_port_list:
  97. restart("otr", p)
  98. for p in soffice_port_list:
  99. if p not in current_port_list:
  100. restart("soffice", p)
  101. kill_soffice()
  102. if __name__ == "__main__":
  103. for i in range(6):
  104. # os.system("echo $(date +%F%n%T)")
  105. monitor()
  106. time.sleep(10)
  107. # kill_defunct()