help-bash
[Top][All Lists]
Advanced

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

[Help-bash] Recommended way to assign trap?


From: Peng Yu
Subject: [Help-bash] Recommended way to assign trap?
Date: Fri, 7 Dec 2018 14:12:53 -0600

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



reply via email to

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