Last active
December 10, 2021 02:27
-
-
Save wrouesnel/4517f5088a2ac84db5dd6a40191610e2 to your computer and use it in GitHub Desktop.
Easy bash "atexit" shutdown hooks.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
shutdown_hooks=() | |
function atexit() { | |
val=$(echo "$@") | |
shutdown_hooks+=("$val") | |
} | |
trap "shutdown" INT TERM | |
function shutdown() { | |
echo "Running shutdown hooks..." | |
for ((i=${#shutdown_hooks[@]}-1; i >=0 ; i--)); do | |
echo "${shutdown_hooks[$i]}" | |
eval "${shutdown_hooks[$i]}" | |
done | |
exit 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a more compact variation, preserving spaces in args, and does not re-eval vars