I'm trying to write a script that will mostly stay idle waiting for a predefined amount of time using regular system sleep
. The problem emerges when I try to kill it externally (with start-stop-daemon
for example). The main process will be killed, but the child sleep will remain in the system until it will run out. I decided to make a sanity trap and kill active sleep
from the script itself. This is how it is done:
cleanup()
{
local PIDS=$(jobs -p)
echo $PIDS
[ -n "$PIDS" ] && kill $PIDS
exit 0
}
trap "cleanup" SIGINT SIGTERM
sleep 1h
When I hit Ctrl-C (send SIGINT) while the script is in foreground, the cleanup() procedure will fire up, but if I'm trying to kill (send default SIGTERM) the running script from other console nothing happens. No the cleanup() is executed, neither script terminates. The script will continue to run just like nothing happens. Can anybody explain what is going on and how to trap external SIGTERM and execute the desired procedure?