bash: sleep process not getting killed
kill -15 22880
will send a signal to the shell executing the script, but not the sleep
command. To send the signal to every process in the process group, you should be able to specify a negative process ID.
kill -15 -22880
Alternately, ensure that the script kills its children before exiting.
trap 'kill $(jobs -p)' EXIT
echo "Sleeping..."
sleep 180s & wait
If you leave sleep
in the foreground when the signal is received, the shell must wait until it exits before running the trap; sleep
is uninterruptible. The workaround is to run it in the background, then wait
on it. wait
, being a shell built-in, can be interrupted, so that the trap runs immediately and kills any background processes still in progress.