help-bash
[Top][All Lists]
Advanced

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

Re: Interrupting scripts with asynchronous subprocesses


From: Chet Ramey
Subject: Re: Interrupting scripts with asynchronous subprocesses
Date: Wed, 3 Jul 2024 11:53:38 -0400
User-agent: Mozilla Thunderbird

On 6/29/24 10:45 PM, Yuri Kanivetsky wrote:
Hi,

I've noticed a strange behavior:

a.sh:

     trap - INT
     while true; do
         echo -n .
         sleep 2
     done

b.sh:

     bash a.sh &
     wait

c.sh:

     {
         trap - INT
         while true; do
             echo -n .
             sleep 2
         done
     } &
     wait

If I interrupt (Ctrl-C) b.sh it leaves a.sh running. That doesn't
happen with c.sh.

b.sh runs a.sh asynchronously, so the shell started to run a.sh ignores
SIGINT and SIGQUIT. a.sh is started with SIGINT ignored, so cannot reset
the disposition to SIG_DFL. That's not the case with c.sh.

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_11 :

"If job control is disabled (see the description of set -m) when the shell
executes an asynchronous list, the commands in the list shall inherit from
the shell a signal action of ignored (SIG_IGN) for the SIGINT and SIGQUIT
signals."

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_28 :

"Signals that were ignored on entry to a non-interactive shell cannot be
trapped or reset, although no error need be reported when attempting to do
so."

But (and there's always a but with POSIX), POSIX decided that under some
circumstances, setting the disposition to SIG_DFL in an asynchronous
command should be allowed:

https://www.austingroupbugs.net/view.php?id=751

so c.sh should allow it.

There was a discussion about this after bash-5.2 came out:

https://lists.gnu.org/archive/html/bug-bash/2023-01/msg00044.html

that resulted in some behavior changes.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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