我的示例代码:
#!/bin/bash
LOGFILE="/tmp/example.log"
cleanup_function() {
echo "Script interrupted by SIGINT, cleaning up..." >> $LOGFILE
exit 0
}
shutdown_function() {
echo "Script terminated by SIGTERM, shutting down..."
exit 0
}
# Traps to handle SIGINT and SIGTERM
trap 'cleanup_function' SIGINT
trap 'shutdown_function' SIGTERM
echo "Script running. It waits for SIGINT or SIGTERM..." >> $LOGFILE
sleep inf
另存为testtrap.sh
在终端中:
$ ./testtrap.sh
在另一个终端中:
$ ps -A | grep testtrap.sh
28923 pts/2 00:00:00 testtrap.sh
$ kill -s SIGINT 28923
$ kill -s SIGTERM 28923
$ ps -A | grep testtrap.sh
28923 pts/2 00:00:00 testtrap.sh
SIGINT
并且SIGTERM
没有被困 etesttrap.sh
没有被他们杀死。怎么了?