help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] Does the parent get the trap signal when the child is ru


From: Peng Yu
Subject: Re: [Help-bash] Does the parent get the trap signal when the child is running in the foreground?
Date: Tue, 11 Dec 2018 13:45:33 -0600

On Tue, Dec 11, 2018 at 10:01 AM Chet Ramey <address@hidden> wrote:
>
> On 12/10/18 12:25 PM, Peng Yu wrote:
> > Hi,
> >
> > I am not sure if I understand the traps related with parent and child
> > processes correctly.
> >
> > I press Ctrl-C 5 times. The child gets SIGINT 5 times and the parent
> > gets 1 once. But since Ctrl-C triggers SIGINT for the process group
> > whose leader is the process started by main1.sh, why main1.sh only
> > gets one signal? Thanks.
>
> The parent receives 5 SIGINTs, but only runs the trap once, when the
> child process completes.
>
> Read https://www.cons.org/cracauer/sigint.html for more insight about the
> options.
>
> The parent shell shouldn't really do anything, since the child -- the
> foreground process -- caught the SIGINT, handled it, and did not exit as a
> result. However, backwards compatibility and user expectations being what
> they are, the parent shell (main1.sh) runs the SIGINT trap as well.

The webpage that you mentioned says bash is WCE "wait and cooperative exit".

"""
WCE The shell exits if a child signaled that it was killed on a signal
(either it had the default handler for SIGINT or it killed itself).
"""

The following test cases match the description.

- ./script_notrap.sh and ./script_resetkilltrap.sh cause the same
behavior of main.sh (exit immediately).
- ./script_emptytrap.sh and ./script_exittrap.sh cause the same
behavior of main.sh (run to the following commands below "$0").

I am not sure about ./script_kill9trap.sh. As the website does not say
what kinds of "kill" it refers to. If it refers to all kinds of
"kill", then the description of the webpage does not match the
behavior of bash.

Should "The shell exits ..." be changed to "the parent shell's INT is
trigger once"?

Also, the implementation of ./script_resetkilltrap.sh still
technically uses the default handler of SIGINT. Is there a way that
the default handler is not used but the subshell kill itself and
causes the parent shell to trigger the INT of the parent shell?

Also, the implementation of this behavior in bash is intriguing to me.
How does the parent shell know whether the child uses the default
handler?

$ ./main.sh ./script_notrap.sh
49186
49187
^C
$ ./main.sh ./script_resetkilltrap.sh
49197
49198
^C

$ ./main.sh ./script_emptytrap.sh
49204
49205
^C./script_emptytrap.sh
0
./main.sh
$ ./main.sh ./script_exittrap.sh
49217
49218
^C130
./main.sh
$ ./main.sh ./script_kill9trap.sh
49227
49228
^C./main.sh: line 5: 49228 Killed: 9               "$1"
137
./main.sh


==> main.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo "$$"
"$1"
echo "$?"
echo "$0"


==> script_emptytrap.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo "$$"
trap '' SIGINT
sleep 3
echo "$0"


==> script_exittrap.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo "$$"
trap 'exit' SIGINT
sleep 3
echo "$0"


==> script_kill9trap.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo "$$"
trap 'kill -9 $$' SIGINT
sleep 3
echo "$0"


==> script_notrap.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo "$$"
sleep 3
echo "$0"


==> script_resetkilltrap.sh <==
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

echo "$$"
trap 'trap - SIGINT; kill -s SIGINT $$' SIGINT
sleep 3
echo "$0"


-- 
Regards,
Peng



reply via email to

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