Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Every cloud engenders not a storm. -- William Shakespeare, "Henry VI"


comp / comp.unix.shell / (bash) Does the EXIT trap fire if/when bash is exited via a signal?

SubjectAuthor
* (bash) Does the EXIT trap fire if/when bash is exited via a signal?Kenny McCormack
`- Re: (bash) Does the EXIT trap fire if/when bash is exited via a signal?Helmut Waitzmann

1
Subject: (bash) Does the EXIT trap fire if/when bash is exited via a signal?
From: Kenny McCormack
Newsgroups: comp.unix.shell
Organization: The official candy of the new Millennium
Date: Wed, 21 Aug 2024 03:19 UTC
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gazelle@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: (bash) Does the EXIT trap fire if/when bash is exited via a signal?
Date: Wed, 21 Aug 2024 03:19:39 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <va3mcb$10662$2@news.xmission.com>
Injection-Date: Wed, 21 Aug 2024 03:19:39 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="1054914"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
View all headers

Note: This thread is entirely about bash. No other shells or "POSIX" are
relevant here. But comparisons between various versions of bash may be
relevant, since I think the behavior may have changed over the versions.

That said, suppose I have something like:

trap 'date > /tmp/somefile' EXIT

# Rest of script

Now, if during "Rest of script", say I hit ^C. Or ^\. Or, say I send a
signal via "kill" from another terminal. Does my exit trap get executed?

I've had varying results. I am pretty sure that at one point, the answer
was "no", but recently, I've noticed that when I exit via ^C, the EXIT trap
does execute. I'm curious what the "official" answer is.

--
The people who tell us to be proud of the white race are the ones who make
us the most embarrassed by it.

Subject: Re: (bash) Does the EXIT trap fire if/when bash is exited via a signal?
From: Helmut Waitzmann
Newsgroups: comp.unix.shell
Organization: A noiseless patient Spider
Date: Fri, 23 Aug 2024 06:31 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: nn.throttle@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: (bash) Does the EXIT trap fire if/when bash is exited via a signal?
Date: Fri, 23 Aug 2024 08:31:36 +0200
Organization: A noiseless patient Spider
Lines: 61
Sender: Helmut Waitzmann <12f7e638@mail.de>
Message-ID: <835xrrwy0n.fsf@helmutwaitzmann.news.arcor.de>
References: <va3mcb$10662$2@news.xmission.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
MIME-Version: 1.0
Content-Type: text/plain; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Date: Fri, 23 Aug 2024 08:41:30 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7815711824038c899b2db8e79c94d8d1";
logging-data="878104"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18F8PL/v+tRMiLEyJDN3SmoXAbu44Fwnt8="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:jyrd5aEQBQ+gFApkiypgTvmXmoU=
sha1:5byjqRNhCTIFTTFqLuCO1nNbNR8=
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
View all headers

gazelle@shell.xmission.com (Kenny McCormack):
> Note: This thread is entirely about bash. No other shells or "POSIX" are
> relevant here. But comparisons between various versions of bash may be
> relevant, since I think the behavior may have changed over the versions.
>
> That said, suppose I have something like:
>
> trap 'date > /tmp/somefile' EXIT
>
> # Rest of script
>
> Now, if during "Rest of script", say I hit ^C. Or ^\. Or, say I send a
> signal via "kill" from another terminal. Does my exit trap get executed?
>
> I've had varying results. I am pretty sure that at one point, the answer
> was "no", but recently, I've noticed that when I exit via ^C, the EXIT trap
> does execute. I'm curious what the "official" answer is.
>

The "official" answer should be in the "bash" documentation, but
anyone who would like to investigate it, might let "bash" run the
following script ("the_script")

#!/bin/sh
trap_command()
{
cat <<EOF
printf 'Executing %s trap at ' ${1:?} &&
date -- '+%F %T'
EOF
}

command="$( trap_command EXIT )" &&
trap "$command" EXIT &&

for signal
do
(
sleep -- 1 &&
printf 'Sending signal %s at ' "$signal" &&
date -- '+%F %T' &&
kill -s "$signal" -- "$$"
) &
sleep -- 3
done

providing signal names as parameters and observe what the shell
will do, for example

(
for signal in INT QUIT HUP TERM
do
bash ./the_script "$signal"
printf 'Script exited returning status %s\n' "$?"
done
)

1

rocksolid light 0.9.8
clearnet tor