I have this script:
#!/bin/bash
set -e
#WHAT SHOULD I WRITE HERE?
sleep 60 # this is for testing
#java ... | logger # this is what will be in the real script
I want to be able to kill myscript and all it's sub-processes by sending myscript a SIGTERM:
$ kill -s SIGTERM 5929
When I run it I see in ps fuxa
:
me 3640 0.0 0.1 108416 2100 pts/2 Ss 09:38 0:00 \_ /bin/bash
me 5929 0.0 0.0 106060 1348 pts/2 S+ 11:21 0:00 \_ /bin/bash myscript
me 5930 0.0 0.0 100908 604 pts/2 S+ 11:21 0:00 \_ sleep 60
I tried numerous trap lines from different answers I found:
#trap 'echo hello' SIGHUP SIGINT SIGTERM EXIT
#trap 'kill $(jobs -p)' SIGHUP SIGINT SIGTERM EXIT
#trap "trap - SIGTERM && kill -- -$$" SIGHUP SIGINT SIGTERM EXIT
I used the 1st one for debugging. What I experience is that I send the SIGTERM (or even more than one), and nothing happens. When I press Ctrl+C
, myscript stops, and it writes "hello" number of times. So this confirms me that the way I am sending the TERM signal is correct, and it is received, but still I did not achieve my goal: to stop myscript immediately. I got the same results with the other 2 trap lines.