1

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.

3
  • I think you mix it with SIGKILL. I did trap TERM, because without the trap line the script exits immediately when I sent the signal, while with the trap 'echo hello' SIGTERM it does not exit, and it prints hello, just not right away Commented Jan 24, 2016 at 14:18
  • Could the sleep be the problem? Any traps you have in place would not be executed before sleep has ended (for more on this, see this question in SO). Commented Jan 30, 2016 at 16:21
  • @SamiLaine, interesting, I'll have a try tomorrow with my original code (java instead of sleep) Commented Jan 30, 2016 at 18:09

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.