简体   繁体   English

bash 陷阱传播到带有自定义信号处理程序的命令

[英]bash trap propagated to command with custom signal handler

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.如果使用sleep而不是./executable_with_custom_signal_handling一切正常。 Otherwise, ./executable_with_custom_signal_handling receives signal immediately in parallel with on_stop .否则, ./executable_with_custom_signal_handling立即与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?任何已知的解决方法?

By default, the shell runs backgrounded commands with SIGINT (and SIGQUIT) ignored .默认情况下, shell 运行后台命令,忽略SIGINT(和 SIGQUIT)

Your backgrounded sleep is not interrupted by the Ctrl-C SIGINT to the process group, then, because it never sees the signal.然后,您的后台sleep不会被进程组的 Ctrl-C SIGINT 中断,因为它永远不会看到信号。 When your custom executable installs a new signal action, replacing SIG_IGN, that executable will receive the SIGINT.当您的自定义可执行文件安装新的信号操作时,替换 SIG_IGN,该可执行文件收到 SIGINT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM