Meu código de exemplo:
#!/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
Salvo comotesttrap.sh
Em um terminal:
$ ./testtrap.sh
Em outro terminal:
$ 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
e SIGTERM
não fica preso e testtrap.sh
não é morto por eles. O que está errado?