Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!usenet.stanford.edu!not-for-mail From: Chet Ramey Newsgroups: gnu.bash.bug Subject: Re: Want way to run background processes with SIGINT unignored Date: Sat, 10 Oct 2015 13:58:43 -0400 Organization: ITS, Case Western Reserve University Lines: 59 Approved: bug-bash@gnu.org Message-ID: References: <22040.2725.561993.552660@chiark.greenend.org.uk> Reply-To: chet.ramey@case.edu NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: usenet.stanford.edu 1444499968 23398 208.118.235.17 (10 Oct 2015 17:59:28 GMT) X-Complaints-To: action@cs.stanford.edu Cc: chet.ramey@case.edu To: Ian Jackson , bug-bash@gnu.org Envelope-to: bug-bash@gnu.org User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <22040.2725.561993.552660@chiark.greenend.org.uk> X-Junkmail-Whitelist: YES (by domain whitelist at mpv1.tis.cwru.edu) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 129.22.105.36 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:11605 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 chet@case.edu http://cnswww.cns.cwru.edu/~chet/