process_killer.py 636 B

1234567891011121314151617181920212223242526
  1. """
  2. @author: luojunhui
  3. """
  4. import subprocess
  5. async def kill_task_by_name(task_name) -> bool:
  6. """
  7. 通过进程名称来杀掉进程
  8. :param task_name:
  9. """
  10. try:
  11. # 构造命令
  12. command = f"ps aux | grep {task_name} | grep -v grep | awk '{{print $2}}' | xargs kill -9"
  13. print(command)
  14. # 执行命令
  15. result = subprocess.run(command, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  16. # 检查执行结果
  17. if result.returncode == 0:
  18. return True
  19. else:
  20. return False
  21. except Exception as e:
  22. return False