我也希望它传播SIGINT信号.
到目前为止,这是我的代码:
#!/bin/bashset -e; set -ufunction child_of { ps --ppID -o "pID" --no-headers | head -n1}function handle_int { echo "Received SIGINT" kill -int $(child_of $SU_PID)}su myuser -p -c "bash /opt/loop.sh 2>&1 | tee -i >(logger -t mytag)" &SU_PID=$!trap "handle_int" SIGINTwait $SU_PIDecho "This is the end."
我的问题是,当我向这个包装器发送一个SIGINT时,handle_int被调用但是脚本结束了,而我希望它继续等待$SU_PID.
有没有办法捕获int信号,做一些事情,然后阻止脚本终止?
解决方法 你有一个问题:在Ctrl-C之后,“这就是结束.”预计但它永远不会到来,因为脚本已经过早退出.原因是在set -e下运行时wait(意外地)返回非零值.根据“男子打击”:
If bash is waiting for a command to complete and receives a signal for which a trap has been set,the trap
will not be executed until the command completes. When bash is waiting for an asynchronous command via the
wait builtin,the reception of a signal for which a trap has been set will cause the wait builtin to return
immediately with an exit status greater than 128,immediately after which the trap is executed.
您应该将set wait包装在set e中,以便在等待异步命令时处理捕获的信号后程序可以继续运行.
像这样:
# wait function that handles trapped signal on asynchronous commands.function safe_async_wait { set +e wait # returns >128 on asynchronous commands set -e}#...safe_async_wait $SU_PID总结
以上是内存溢出为你收集整理的linux – 防止bash脚本在处理SIGINT后终止全部内容,希望文章能够帮你解决linux – 防止bash脚本在处理SIGINT后终止所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)