kill_all.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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
  7. ip_port_dict = get_ip_port()
  8. ip = "http://" + get_intranet_ip()
  9. python_path = ip_port_dict.get(ip).get("python_path")
  10. project_path = ip_port_dict.get(ip).get("project_path")
  11. def kill():
  12. if python_path and project_path:
  13. pid_list = psutil.pids()
  14. for pid in pid_list:
  15. process = psutil.Process(pid)
  16. process_cmd = ''
  17. for c in process.cmdline():
  18. process_cmd += c + " "
  19. if process_cmd.strip() == "":
  20. continue
  21. if "monitor" in process_cmd or "kill" in process_cmd:
  22. continue
  23. if re.search(project_path, process_cmd):
  24. comm = "kill -9 " + str(pid)
  25. print(comm, process_cmd)
  26. os.system(comm)
  27. if re.search("gunicorn", process_cmd):
  28. comm = "kill -9 " + str(pid)
  29. print(comm, process_cmd)
  30. os.system(comm)
  31. else:
  32. print("cannot kill! checkout config...")
  33. print(ip_port_dict)
  34. print(ip, python_path, project_path)
  35. if __name__ == "__main__":
  36. kill()