我有一个python脚本myscript.py
subprocess.Popen(my_command, shell=True)我从我的终端运行python3 myscript.py,现在我无法停止使用ctrl + c运行的my_command。my_command将继续运行。反正我能阻止它吗?
谢谢!
发布于 2022-02-02 22:31:23
可以使用psutil
import subprocess
import psutil
def destroyed(proc_pid):
process = psutil.Process(proc_pid)
for proc in process.children(recursive=True):
proc.kill()
process.kill()
proc = subprocess.Popen(["sleep", " 1000"], shell=True)
try:
proc.wait(timeout=5)
except subprocess.TimeoutExpired:
destroyed(proc.pid)https://stackoverflow.com/questions/70963684
复制相似问题