假设我有这个 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_script
SIGINT
SIGTERM
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)
回答我问题的人证明我错了。
但它不起作用,因为 …