Skip to content

Instantly share code, notes, and snippets.

@x-yuri
Last active July 2, 2024 03:08
Show Gist options
  • Save x-yuri/bc2d806e1e6fedce84532b0762341535 to your computer and use it in GitHub Desktop.
Save x-yuri/bc2d806e1e6fedce84532b0762341535 to your computer and use it in GitHub Desktop.
bash: interrupting async subprocesses

bash: interrupting async subprocesses

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
$ bash b.sh
..
$ ps -eHo pid,ignored,args
    PID          IGNORED COMMAND
...
     52 0000000000000004   bash b.sh
     53 0000000000000006     bash a.sh
     54 0000000000000006       sleep 2
..^C
$ .ps -ef.
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 13:09 pts/0    00:00:00 /bin/sh
root          41       0  0 13:15 pts/1    00:00:00 sh
root          53       1  0 13:16 pts/0    00:00:00 bash a.sh
root          59      53  0 13:16 pts/0    00:00:00 sleep 2
root          60       1  0 13:16 pts/0    00:00:00 ps -ef
$ ki.ll .53.

$ bash c.sh
..
$ ps -eHo pid,ignored,args
    PID          IGNORED COMMAND
...
     65 0000000000000004   bash c.sh
     66 0000000000000004     bash c.sh
     67 0000000000000000       sleep 2
..^C
$ ps -ef
UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 13:09 pts/0    00:00:00 /bin/sh
root          41       0  0 13:15 pts/1    00:00:00 sh
root          71       1  0 13:17 pts/0    00:00:00 ps -ef

Apparently bash tries to implement WCE behavior in terms of this paper, but for some reason in the b.sh case sleep inherits the SIG_IGN handler which doesn't let a.sh exit, and in the c.sh case this doesn't happen.

https://lists.gnu.org/archive/html/help-bash/2024-06/msg00019.html
https://lists.gnu.org/archive/html/help-bash/2024-07/msg00000.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment