bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Want way to run background processes with SIGINT unignored


From: Ian Jackson
Subject: Want way to run background processes with SIGINT unignored
Date: Fri, 9 Oct 2015 19:42:45 +0100

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.

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 suggest that this option should have two possible modes:

 1. Current behaviour (the default): SIGINT and SIGQUIT are set to
    SIG_IGN in the child shortly after fork.

 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.

If this sounds like a good idea, I'm happy to write the patch.  Please
tell me what these options should be called :-).  I suggest:

  set -o nobgchildsigint   # posix behaviour
  set -o bgchildsigint     # reset to shell's inherited dispositions

I suggest that these options do the same for both signals.  Ie that it
wouldn't be possible to specify the inheritance separately for INT and
QUIT.

This whole discussion, and these proposed options, are relevant only
when job control is not in effect.


Thanks,
Ian.


[1]
http://lists.xenproject.org/archives/html/xen-devel/2015-10/msg01208.html   
http://lists.xenproject.org/archives/html/xen-devel/2015-10/msg01211.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]