Sample Header Ad - 728x90

What determines whether a script's background processes get a terminal's SIGINT signal?

7 votes
2 answers
2208 views
#!/usr/bin/env bash

sleep 3 && echo '123' &
sleep 3 && echo '456' &
sleep 3 && echo '999' &
If I run this, and send SIGINT by pressing control-c via terminal, it seems to still echo the 123... output. I assumed this is because it's somehow detached? However if I add a wait < <(jobs -p) (wait for all background jobs to finish) to the end of the script, if I run it then, and send the SIGINT then the 123... output is **not** displayed. What explains this behavior? Is wait somehow intercepting the signal and passing it to the background processes? Or is it do with some sort of state of whether a process is "connected" or not to a terminal? I found one possibly relevant question on this but I couldn't figure out how it relates to the above behaviour: https://unix.stackexchange.com/questions/564726/why-sigchld-signal-was-not-ignored-when-using-wait-functions
Asked by Chris Stryczynski (6603 rep)
Jun 21, 2020, 10:18 AM
Last activity: Jun 30, 2024, 11:24 PM