【发布时间】:2018-01-26 14:06:44
【问题描述】:
我用一个 shell 脚本多次启动一个 python 脚本。一些python脚本运行后,我想从while循环中跳出来。在我当前的解决方案中,我从 python 为 shell 脚本 PID 发送一个终止信号。但我想防止父进程在子进程完成之前死亡。
我当前的shell脚本:
#!/bin/sh
while true
do
python3 my_py_script.py $$
done
我的python脚本的相关部分:
import signal
import sys
...
shell_script_pid = int(sys.argv[1])
...
if ..something..:
os.kill(shell_script_pid, signal.SIGTERM)
sys.exit('python script end')
【问题讨论】:
-
您不能将 Python 代码包装在一个 while 循环中并管理您的 Python 代码中的迭代吗?
-
为什么需要在您的子进程之一完成之前阻止父进程退出?
-
@Evan 防止僵尸进程。
-
@franciscosollima 这也是一个解决方案。但我更喜欢每次运行都使用新的流程。
标签: python python-3.x shell