0

In my script I'm trapping signals in the usual way.

function on_stop {
  echo 'On Stop'
  sleep 10
  echo 'Signalling others to exit'
  trap - TERM EXIT INT
  kill -s INT "$$"
}

./executable_with_custom_signal_handling &
pid=$!
trap 'on_stop' TERM EXIT INT
wait

If sleep is used instead of ./executable_with_custom_signal_handling everything works as expected. Otherwise, ./executable_with_custom_signal_handling receives signal immediately in parallel with on_stop.

I am wondering does it have something to do with a custom signal handling in the executable?

signal(SIGINT, handler)

Any workarounds known?

Yuki
  • 3,857
  • 5
  • 25
  • 43
  • Does this answer your question? [bash: Why can't I set a trap for SIGINT in a background shell?](https://stackoverflow.com/questions/46061694/bash-why-cant-i-set-a-trap-for-sigint-in-a-background-shell) – pilcrow Feb 10 '20 at 16:17
  • Why does it work with `sleep 2000 &`? – Yuki Feb 10 '20 at 16:37
  • I tried without background, still not working as expected. – Yuki Feb 10 '20 at 16:39

1 Answers1

0

By default, the shell runs backgrounded commands with SIGINT (and SIGQUIT) ignored.

Your backgrounded sleep is not interrupted by the Ctrl-C SIGINT to the process group, then, because it never sees the signal. When your custom executable installs a new signal action, replacing SIG_IGN, that executable will receive the SIGINT.

pilcrow
  • 56,591
  • 13
  • 94
  • 135