monitor_process3.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import logging
  2. import os
  3. import re
  4. import sys
  5. import time
  6. import psutil
  7. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  8. from format_convert.utils import get_ip_port
  9. # convert_port_list = ["15010"]
  10. # ocr_port_list = ["15011", "15013", "15015"]
  11. # ocr_port_list = ["15011", "15013", "15015", "15017", "15019"]
  12. # otr_port_list = ["15012", "15014", "15016", "15018", "15020"]
  13. # ocr_port_list = ["15011", "15013", "15015", "15017", "15019", "15021"]
  14. # otr_port_list = ["15012", "15014", "15016", "15018", "15020", "15022"]
  15. # soffice_port_list = ["16000", "16001", "16002", "16003", "16004", "16005",
  16. # "16006", "16007", "16008", "16009"]
  17. convert_port_list = get_ip_port("convert")
  18. ocr_port_list = get_ip_port("ocr")
  19. otr_port_list = get_ip_port("otr")
  20. soffice_port_list = get_ip_port("office")
  21. python_path = "/root/miniconda3/bin/python"
  22. interface_path = "/data/format_conversion_maxcompute"
  23. std_out = " >>/convert.out 2>&1 &"
  24. std_out_gpu = " >>/gpu.out 2>&1 &"
  25. convert_comm = "nohup " + python_path + " " + interface_path + "/format_convert/convert.py #" + std_out
  26. ocr_comm = "nohup " + python_path + " " + interface_path + "/ocr/ocr_interface.py # 0" + std_out + std_out_gpu
  27. otr_comm = "nohup " + python_path + " " + interface_path + "/otr/otr_interface.py # 0" + std_out + std_out_gpu
  28. soffice_comm = "docker run -itd -p #:16000 soffice:v1 bash"
  29. def get_port():
  30. net_conn = psutil.net_connections()
  31. current_port_list = []
  32. for conn in net_conn:
  33. current_port_list.append(str(conn.laddr.port))
  34. current_port_list = list(set(current_port_list))
  35. current_port_list.sort(key=lambda x: x)
  36. # print(current_port_list)
  37. return current_port_list
  38. def restart(process_type, port):
  39. if process_type == "convert":
  40. _comm = re.sub("#", port, convert_comm)
  41. elif process_type == "ocr":
  42. _comm = re.sub("#", port, ocr_comm)
  43. elif process_type == "otr":
  44. _comm = re.sub("#", port, otr_comm)
  45. elif process_type == "soffice":
  46. _comm = re.sub("#", port, soffice_comm)
  47. else:
  48. _comm = "netstat -nltp"
  49. print("no process_type", process_type)
  50. print(_comm)
  51. # os.system("netstat -nltp")
  52. os.system("echo $(date +%F%n%T)")
  53. os.system(_comm)
  54. def kill_soffice(limit_sec=12):
  55. pid_list = psutil.pids()
  56. for pid in pid_list:
  57. process = psutil.Process(pid)
  58. if re.search("soffice", process.exe()):
  59. start_time = process.create_time()
  60. now_time = time.time()
  61. # run_time = process.cpu_times().user
  62. run_time = now_time-start_time
  63. if run_time >= limit_sec:
  64. comm = "kill -9 " + str(pid)
  65. print("kill process ", str(pid), str(process.exe()), str(run_time), ">", limit_sec)
  66. os.system("echo $(date +%F%n%T)")
  67. os.system(comm)
  68. def monitor():
  69. current_port_list = get_port()
  70. # for p in convert_port_list:
  71. # if p not in current_port_list:
  72. # restart("convert", p)
  73. for p in ocr_port_list:
  74. if p not in current_port_list:
  75. restart("ocr", p)
  76. for p in otr_port_list:
  77. if p not in current_port_list:
  78. restart("otr", p)
  79. # for p in soffice_port_list:
  80. # if p not in current_port_list:
  81. # restart("soffice", p)
  82. #
  83. # kill_soffice()
  84. if __name__ == "__main__":
  85. monitor()