简短的回答:bash 中的 SIGINT 可以被捕获、处理然后忽略,假设这里的“忽略”意味着 bash 继续运行脚本。处理程序的所需操作甚至可以推迟以构建一种“事务”,以便在一组语句完成工作后触发(或“忽略”) SIGINT。
但是由于上面的示例涉及 bash 的许多方面(前台与后台行为、陷阱和等待)并且从那时起已经过去了 8 年,因此这里讨论的解决方案可能无法在没有进一步微调的情况下立即适用于所有系统。
此处讨论的解决方案已在具有“GNU bash,版本 4.4.20(1)-release”的“Linux mint-mate 5.4.0-73-generic x86_64”系统上成功测试:
wait
shell 内置命令被设计为可中断的。但是可以检查 的退出状态wait
,即 128 + 信号数 = 130(在 SIGINT 的情况下)。因此,如果您想欺骗并等待后台进程真正完成,也可以执行以下操作:
wait ${programPID}
while [ $? -ge 128 ]; do
# 1st opportunity to place your **handler actions** is here
wait ${programPID}
done
但它也说我们在测试所有这些时遇到了一个错误/功能。问题是wait
即使在后台进程不再存在之后仍然返回 130。文档说,wait
如果进程 ID 错误,它将返回 127,但这在我们的测试中没有发生。wait
如果您也遇到此问题,请记住在 while 循环中运行命令之前检查后台进程的存在。
- 假设下面的脚本是你的
program
,它只是从 5 倒数到 0 并且 tee 的输出到名为 program.out 的文件中。这里的while循环被认为是一个“事务”,它不会被SIGINT干扰。最后一条评论:此代码在执行延迟操作后不会忽略 SIGINT,而是恢复旧的 SIGINT 处理程序并引发 SIGINT:
#!/bin/bash
rm -f program.out
# Will be set to 1 by the SIGINT ignoring/postponing handler
declare -ig SIGINT_RECEIVED=0
# On <CTRL>+C or "kill -s SIGINT $$" set flag for [later|postponed] examination
function _set_SIGINT_RECEIVED {
SIGINT_RECEIVED=1
}
# Remember current SIGINT handler
old_SIGINT_handler=$(trap -p SIGINT)
# Prepare for later restoration via ${old_SIGINT_handler}
old_SIGINT_handler=${old_SIGINT_handler:-trap - SIGINT}
# Start your "transaction", which should NOT be disturbed by SIGINT
trap -- '_set_SIGINT_RECEIVED' SIGINT
count=5
echo $count | tee -a program.out
while (( count-- )); do
sleep 1
echo $count | tee -a program.out
done
# End of your "transaction"
# Look whether SIGINT was received
if [ ${SIGINT_RECEIVED} -eq 1 ]; then
# Your **handler actions** are here
echo "SIGINT was received during transaction..." | tee -a program.out
echo "... doing postponed work now..." | tee -a program.out
echo "... restoring old SIGINT handler and sending SIGINT" | tee -a program.out
echo "program finished after SIGINT postponed." | tee -a program.out
${old_SIGINT_handler}
kill -s SIGINT $$
fi
echo "program finished without having received SIGINT." | tee -a program.out
但是这里也说一下,我们program
在后台发送后遇到了问题。问题是program
继承了 atrap '' SIGINT
这意味着 SIGINT 通常被忽略并且program
无法通过trap -- '_set_SIGINT_RECEIVED' SIGINT
.
- 我们通过放入
program
一个子shell 并在后台发送这个子shell 解决了这个问题,正如您现在将MAIN
在前台运行的脚本示例中看到的那样。最后还有一条评论:在此脚本中,您可以通过变量决定ignore_SIGINT_after_handling
是否最终忽略 SIGINT 并继续运行脚本,或者在您的处理程序操作完成其工作后执行默认的 SIGINT 行为:
#!/bin/bash
# Will be set to 1 by the SIGINT ignoring/postponing handler
declare -ig SIGINT_RECEIVED=0
# On <CTRL>+C or "kill -s SIGINT $$" set flag for later examination
function _set_SIGINT_RECEIVED {
SIGINT_RECEIVED=1
}
# Set to 1 if you want to keep bash running after handling SIGINT in a particular way
# or to 0 (or any other value) to run original SIGINT action after postponing SIGINT
ignore_SIGINT_after_handling=1
# Remember current SIGINT handler
old_SIGINT_handler=$(trap -p SIGINT)
# Prepare for later restoration via ${old_SIGINT_handler}
old_SIGINT_handler=${old_SIGINT_handler:-trap - SIGINT}
# Start your "transaction", which should NOT be disturbed by SIGINT
trap -- '_set_SIGINT_RECEIVED' SIGINT
# Do your work, for eample
(./program) &
programPID=$!
wait ${programPID}
while [ $? -ge 128 ]; do
# 1st opportunity to place a part of your **handler actions** is here
# i.e. send SIGINT to ${programPID} and make sure that it is only sent once
# even if MAIN receives more SIGINT's during this loop
wait ${programPID}
done
# End of your "transaction"
# Look whether SIGINT was received
if [ ${SIGINT_RECEIVED} -eq 1 ]; then
# Your postponed **handler actions** are here
echo -e "\nMAIN is doing postponed work now..."
if [ ${ignore_SIGINT_after_handling} -eq 1 ]; then
echo "... and continuing with normal program execution..."
else
echo "... and restoring old SIGINT handler and sending SIGINT via 'kill -s SIGINT \$\$'"
${old_SIGINT_handler}
kill -s SIGINT $$
fi
fi
# Restore "old" SIGINT behaviour
${old_SIGINT_handler}
# Prepare for next "transaction"
SIGINT_RECEIVED=0
echo ""
echo "This message has to be shown in the case of normal program execution"
echo "as well as after a caught and handled and then ignored SIGINT"
echo "End of MAIN script received"
希望这个对你有帮助。愿大家都过得愉快。