0

I have the following script s2:

#!/bin/bash
trap 't' INT
function t() {
        echo "trap"
}
sleep 999

Then I am calling s1:

#!/bin/bash
./s2 &
wait

So s1 waits for s2 which sleeps. Now when I press Ctrl+C it does not print the trap message in s2. Why is that?

Moreover if in s1 you kill s2 explicitly after launching it with INT signal, it still wont call the interrupt.

#!/bin/bash
./s2 &
pid=$!
kill -INT $pid
wait # waits for s2 instead of killing it right away

Why?

jam
  • 803
  • 5
  • 14
  • Ctrl-C is killing the foreground process group, not just the process. Bash waits for `sleep` to exit because it assumes that this process has also everyone a sigint, and it's waiting to see if it's handled or not – that other guy Nov 01 '17 at 02:19

1 Answers1

0

There are two questions here. I'm not sure about the answer to the second question, but I can tell you what's going on in your first example.

  1. you launch s1
  2. s2 is launched by s1
  3. s1 is waiting for s2
  4. you kill s1
  5. s2 is still running

You can confirm this by checking your running processes.

Your second example seems a bit odd to me. I'll update my answer if I figure it out.

Erik B
  • 40,889
  • 25
  • 119
  • 135
  • No actually I tried simplifying the issue I had but my simplification is wrong. This does not print the message because it creates a distinct shell that we have no visibility over. The reason why it doesnt kill it is I didnt put exit in the trap. My issue is more complicated. I have a ssh remote call in the second script which the trap for some reason does not kill and I'm trying to figure out why. I'll repost my question. – jam Nov 01 '17 at 16:53