相关疑难解决方法(0)

当 SIGINT 或 SIGTERM 发送到父脚本本身而不是子进程时执行命令或函数

假设我有这个 script.sh

#!/bin/bash
exit_script() {
    echo "Printing something special!"
    echo "Maybe executing other commands!"
    kill -- -$$ # Sends SIGTERM to child/sub processes
}

echo "Some other text"
#other commands here
sleep infinity
Run Code Online (Sandbox Code Playgroud)

我想在它收到或 例如时script.sh执行该函数:exit_scriptSIGINTSIGTERM

killall script.sh # it will send SIGTERM to my script
Run Code Online (Sandbox Code Playgroud)

我希望我的脚本执行这个

exit_script() {
    echo "Printing something special!"
    echo "Maybe executing other commands!"
    kill -- -$$ # Sends SIGTERM to child/sub processes
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用 trap

trap exit_script SIGINT SIGTERM
Run Code Online (Sandbox Code Playgroud)

回答我问题的人证明我错了。
但它不起作用,因为 …

scripting bash process signals shell-script

14
推荐指数
1
解决办法
2万
查看次数

标签 统计

bash ×1

process ×1

scripting ×1

shell-script ×1

signals ×1