[英]Stop execution of python script when parent Bash shell script is killed
I'm working on a Bash shell script that runs several Python scripts like so: 我正在研究一个运行几个Python脚本的Bash shell脚本,如下所示:
cd ${SCRIPT_PATH}
python -u ${SCRIPT_NAME} ${SCRIPT_ARGS} >> $JOBLOG 2>&1
At one point, I killed the shell script (using kill PID
), but the Python script continued running, even after the script terminated. 有一次,我杀死了shell脚本(使用
kill PID
),但Python脚本继续运行,即使在脚本终止后也是如此。 I thought these would die as soon as the main script died. 我以为这些会在主剧本死后立即死亡。 What am I misunderstanding about Bash scripting, and what can I do to get the functionality I'm looking for?
我对Bash脚本的误解是什么,我该怎么做才能获得我正在寻找的功能? Thanks in advance!
提前致谢!
你需要安装一个信号处理程序把你的子进程的护理 :
trap "echo killing childs; pkill -P $$" EXIT
Children should be sent SIGHUP when the parent process dies - however: 当父进程死亡时,应该向孩子发送SIGHUP - 但是:
a) The child process can ignore SIGHUP, or handle it a non-fatal manner. a)子进程可以忽略SIGHUP,或以非致命的方式处理它。
b) The Child could disassociate itself from the parent process by fork() and becoming a process group leader. b)Child可以通过fork()与父进程取消关联,并成为进程组组长。
You could just exec
the python code, so that the shell is replaced with the python. 你可以只
exec
python代码,以便用python替换shell。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.