kill_all.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import os
  2. import re
  3. import sys
  4. import psutil
  5. sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../")
  6. from format_convert.utils import get_ip_port, get_intranet_ip, get_using_ip, get_args_from_config
  7. import time
  8. ip_port_dict = get_ip_port()
  9. ip = get_using_ip()
  10. if ip == 'http://127.0.0.1':
  11. ip = 'http://0.0.0.0'
  12. python_path = get_args_from_config(ip_port_dict, ip, "python_path")[0]
  13. project_path = get_args_from_config(ip_port_dict, ip, "project_path")[0]
  14. gunicorn_path = get_args_from_config(ip_port_dict, ip, "gunicorn_path")[0]
  15. def kill():
  16. if python_path and project_path:
  17. pid_list = psutil.pids()
  18. for pid in pid_list:
  19. try:
  20. process = psutil.Process(pid)
  21. except:
  22. continue
  23. process_cmd = ''
  24. for c in process.cmdline():
  25. process_cmd += c + " "
  26. if process_cmd.strip() == "":
  27. continue
  28. if "monitor" in process_cmd or "kill" in process_cmd:
  29. continue
  30. if re.search(project_path, process_cmd):
  31. comm = "kill -9 " + str(pid)
  32. print(comm, process_cmd)
  33. os.system(comm)
  34. else:
  35. print("cannot kill! checkout config...")
  36. print(ip_port_dict)
  37. print(ip, python_path, project_path)
  38. if __name__ == "__main__":
  39. for _ in range(3):
  40. kill()
  41. time.sleep(0.5)