help-bash
[Top][All Lists]
Advanced

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

Re: [Question] Reliable way to clean up a directory in EXIT trap


From: Koichi Murase
Subject: Re: [Question] Reliable way to clean up a directory in EXIT trap
Date: Tue, 19 Jul 2022 18:56:10 +0900

2022年7月19日(火) 18:04 Masahiro Yamada <masahiroy@kernel.org>:
> Here is even simpler test code.

Here is a reduced case:

$ bash -c 'for i in {0..999}; do (trap "rm -rf $BASHPID.tmp" EXIT; >>
"$BASHPID.tmp") done'
^C

I'm not sure if we should clean up temporary files on the signals in
general. For example, it is also possible that we would like to check
the intermediate state of the processing after canceling the command
by INT, for which we want to preserve the intermediate state of the
temporary files. Naively, I would expect programs to terminate without
changing the current status of the filesystem by default. When you
would like to clean up the temporary files on INT, I think that means
you actually would like to override the behavior of INT. For this
reason, I feel you should just trap INT as

for i in {0..999}; do
  (
    tmp=$BASHPID.tmp
    cleanup() { rm -rf "$tmp"; }
    trapint() { cleanup; trap - INT; kill -INT "$BASHPID"; }
    trap cleanup EXIT
    trap trapint INT
    >> "$tmp"
  )
done

See also https://www.cons.org/cracauer/sigint.html for proper handling of INT.

--
Koichi



reply via email to

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