I have a code that looks like this:
{
trap cleanup SIGHUP SIGINT SIGTERM
function executed() {
if [ ${1} -ne 0 ]
then
echo "Failed!"
cleanup
exit
else
echo "Succeeded!"
fi
}
function cleanup() {
echo "Cleaning up ..."
# deleting some files and stuff like that
}
bunch of code ...
...
} 2>&1 | while IFS= read -r line; do echo "$(date) $line"; done \
>> ldapfe_graceful_$(date '+%Y_%m_%d__%H_%M_%S').log
I can't paste all my code since I'm not sure about company policy on this and all that, but the most important parts are there.
I press Ctrl + c while this script is running and it either stops immediately, or I'm shown a little ^C and then it stops after 5-10 seconds, but the cleanup function is never called.
I've put all of the code inside {} to redirect it nicely in a file (I've used seperate script to call this one and do the redirecting but it seems harder to kill that one and propagate the signal to this one so I've just decided to put it all in curly braces here).
I'm sure I'm missing something regarding the curly braces because if I put trap and cleanup outside of the {} block the cleanup function is called, but other parts of the code should be able to call it too and it's not possible if it's outside of the {} braces.
EDIT: I've tried declaring trap after the cleanup function, but no change.