linux - bash:可能需要双 Ctrl-c 才能退出脚本?

标签 linux bash shell

最终目标:WAITING后台作业完成的 BASH 脚本不会在第一次 Ctrl-c 时中止;相反,它需要第二次 Ctrl-c 才能退出。

我很清楚 BASH 内置的 trap 是如何工作的。您可以:

  1. 用它来完全忽略一个信号(例如,trap '' 2)……或者

  2. 使用它在信号原函数允许发生之前执行任意命令(例如,trap cmd 2,其中 cmd 在父级之前运行脚本将因 SIGINT)

  3. 而中断

所以问题归结为:

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 只能从上面执行 12

#!/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/

相关文章:

bash - Linux 路径中的斜杠何时需要在 bash 脚本中放在引号之外

bash - 最接近值的不同文件,具有不同的行数和其他条件( bash awk 其他)

shell - shell脚本如何从文件中读取数据

linux - 如何在脚本中的另一台服务器上运行 shell 脚本?

bash - FFmpeg 脚本不循环

linux - iptables端口转发: Unknown arg

linux - 在 bash 中迭代数据文件

linux - 唯一的文件 ID?

json - 使用 jq 从数组中选择特定项目

linux - 获取当前日期并将其用于文件名