Discussion:
Recommended way to assign trap?
(too old to reply)
Peng Yu
2018-12-07 20:12:53 UTC
Permalink
Hi,

Sometimes, the trapped code (e.g., for clean-up) takes time to run. If
the trap is not disabled, the clean-up code may not get finished if a
2nd signal comes.

I'm wondering what is the recommended practice to program for traps. I
come up with the following to always reset the trap at the beginning
so that I will not respond to new traps until the clean up code in the
original trap is finished. Is this appropriate? Thanks.

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

trap 'echo SIGINT start; trap "" SIGINT; sleep 10; echo SIGINT end' SIGINT
set -v
sleep 60
$ ./main.sh
sleep 60
^Cecho SIGINT start; trap "" SIGINT; sleep 10; echo SIGINT end
SIGINT start
^C^C^CSIGINT end
--
Regards,
Peng
Loading...