On 10/9/15 2:42 PM, Ian Jackson wrote: > Hi. > > I've been wrestling recently[1] with a bash script which invokes a > number of subprocesses in parallel and collects the output. The > problem is that if you ^C the script, the subprocesses carry on > running. This is because of the standards-mandated resetting of > SIGINT (and QUIT) to SIG_IGN in children. > > Working around this in a race-free way with additional code in the > script is very hard indeed. I can't see how to do it without having > the parent install an INT trap handler which synchronises with all the > children, or something equally baroque. > > > The reason for SIGINT being ignored is purely historical: back in the > dawn of time, there was no job control. If you interactively spawned > a background process with &, you wouldn't want your attempts to ^C > your foreground process to kill it. This SIGINT-ignoring also applied > to noninteractive shells and of course came to be relied on. So it is > too late to change the default :-/. > > > However, it would be very easy for bash to provide an option (via `set > -o' perhaps) to disable this behaviour. That is, to allow SIGINT to > be delivered normally to child processes.
I'm restricting non-standard options to `shopt' to avoid any possible conflict with future posix changes. > > With such an option, scripts which run on modern systems and which > attempt to parallelise their work, would be able to arrange that ^C > properly cleans up the whole process group, rather than leaving the > background tasks running (doing needless work and perhaps causing > lossage). I'd be willing to look at a patch that implemented a new option to enable this. You only need one option; you only have two behavior modes and you're introducing one new behavior. > 2. In the child, reset SIGINT and SIGQUIT to the values found at > shell startup. That is, uninstall trap handlers. This is what > most ordinary scripts will want, because they don't want the trap > handler running in both parent and child. It's the same as is > done for all other signals, and for all signals in non-background > subshells and subprocesses. This is the behavior that any new option would toggle. Some name like `async_sig_ignore' or `async_sig_restore' would work. Chet -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, ITS, CWRU c...@case.edu http://cnswww.cns.cwru.edu/~chet/