提问人:amin 提问时间:9/28/2023 更新时间:9/28/2023 访问量:34
如何通过子进程正确终止在后台运行的进程?
How to properly terminate a process that is running in the background via subprocess?
问:
我有方法通过以下方式在后台运行 bash 脚本:subprocess.Popen()
def my_method():
try:
process = subprocess.Popen(["bash", "my_script.sh", "&" ],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
except Exception as e:
print(f"Error running the script: {e}")
return process
def caller():
process = my_method()
现在,我希望该方法能够正确终止后台脚本。我尝试了几种解决方案,例如 和 .但是,它们都不起作用。我想知道这是最好的解决方案吗?caller
process.kill()
process.terminate()
答: 暂无答案
评论
"&"
process
"&"
Popen
Popen