最终目标:WAITING后台作业完成的 BASH 脚本不会在第一次 Ctrl-c
时中止;相反,它需要第二次 Ctrl-c
才能退出。
我很清楚 BASH 内置的 trap
是如何工作的。您可以:
用它来完全忽略一个信号(例如,
trap '' 2
)……或者使用它在信号原函数允许发生之前执行任意命令(例如,
trap cmd 2
,其中cmd
在父级之前运行脚本将因SIGINT
) 而中断
所以问题归结为:
How can I effectively combine 1 & 2 together, i.e., prevent the end-result a signal would lead to (1 -- e.g., stop script cancelling due to
SIGINT
) while also making that signal cause something else (2 -- e.g., increment a counter, check the counter and conditionally either print a warning or exit).
更简单地说:
How can I make a signal do something else entirely; not just insert a job before it does its thing.
这里有一些示例代码来演示我的目标;然而,它当然不起作用——因为 trap
只能从上面执行 1 或 2。
#!/bin/bash
declare -i number_of_times_trap_triggered
cleanup_bg_jobs() {
number_of_times_trap_triggered+=1
if [[ ${number_of_times_trap_triggered} -eq 1 ]]; then
echo "There are background jobs still running"
echo "Hit Ctrl-c again to cancel all bg jobs & quit"
else
echo "Aborting background jobs"
for pid in ${bg_jobs}; do echo " Killing ${pid}"; kill -9 ${pid}; done
fi
}
f() { sleep 5m; }
trap cleanup_bg_jobs 2
bg_jobs=
for job in 1 2 3; do
f &
bg_jobs+=" $!"
done
wait
这就是当您按一次 Ctrl-c
时最终得到的输出。
[rsaw:~]$ ./zax
^CThere are background jobs still running
Hit Ctrl-c again to cancel all bg jobs & quit
[rsaw:~]$ ps axf|tail -6
24569 pts/3 S 0:00 /bin/bash ./zax
24572 pts/3 S 0:00 \_ sleep 5m
24570 pts/3 S 0:00 /bin/bash ./zax
24573 pts/3 S 0:00 \_ sleep 5m
24571 pts/3 S 0:00 /bin/bash ./zax
24574 pts/3 S 0:00 \_ sleep 5m
当然,我可以修改它以清除第一个 Ctrl-c
上的作业,但这不是我想要的。我想阻止 BASH 在第一次陷阱被触发后退出……直到它被第二次触发。
PS:目标平台是带有 BASH v4+ 的 Linux(我不太关心 POSIX 合规性)
最佳答案
我有一个稍微不同的用例,并且想在这里留下解决方案,因为谷歌引导我进入这个主题。您可以继续运行一个命令,并允许用户使用一次 CTRL+C
重新启动它,然后使用两次 CTRL+C
终止它,方法如下:
trap_ctrlC() {
echo "Press CTRL-C again to kill. Restarting in 2 second"
sleep 2 || exit 1
}
trap trap_ctrlC SIGINT SIGTERM
while true; do
... your stuff here ...
done
关于linux - bash:可能需要双 Ctrl-c 才能退出脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24829213/