help-bash
[Top][All Lists]
Advanced

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

Interrupting scripts with asynchronous subprocesses


From: Yuri Kanivetsky
Subject: Interrupting scripts with asynchronous subprocesses
Date: Sun, 30 Jun 2024 13:45:17 +0300

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.

It looks like it has something to do with sleep ignoring SIGINT. With
c.sh it doesn't and the subshell terminates:

https://git.savannah.gnu.org/cgit/bash.git/tree/jobs.c?h=bash-5.2#n4102

With b.sh it ignores SIGINT, and a.sh decides to stay.

Why the different behavior? Because in case of b.sh
restore_original_signals() (when invoking sleep):

https://git.savannah.gnu.org/cgit/bash.git/tree/trap.c?h=bash-5.2#n1388

restores SIGINT to 1 (SIG_IGN), in case of c.sh to 0.

Probably because in case of b.sh the second process starts from the beginning:

https://git.savannah.gnu.org/cgit/bash.git/tree/shell.c?h=bash-5.2#n1948

Which doesn't happen in case of c.sh.

And although I provided this explanation, can you explain what's going
on here? Sort of, what's the rule?

Regards,
Yuri



reply via email to

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