当我的顶级脚本退出时,我正在寻找一种清理混乱的方法.
特别是如果我想使用set -e,我希望后台进程会在脚本退出时死掉.
收到信号后,我可以使用执行某些命令trap.例:
trap 'echo hello world' 1 2
Run Code Online (Sandbox Code Playgroud)
如果收到任何指定的信号,则显示"hello world".
但是如何打印/识别收到的信号名称?
我正在开发一个管理一些陷阱的脚本。一开始我只用这段代码管理 INT 和 SIGTSTP,它运行得很好:
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps INT
trap capture_traps SIGTSTP
read -p "Script do its stuff here and we use read for the example we pause for time to generate trap event"
exit 0
Run Code Online (Sandbox Code Playgroud)
然后我尝试添加我想要管理的新陷阱,即 SIGINT 和 SIGHUP。首先,我这样做了(这是有效的):
#!/bin/bash
function capture_traps() {
echo -e "\nDoing something on exit"
exit 1
}
trap capture_traps INT
trap capture_traps SIGTSTP
trap capture_traps SIGINT
trap capture_traps SIGHUP
read -p "Script do …Run Code Online (Sandbox Code Playgroud)