Skip to content

Instantly share code, notes, and snippets.

@wrouesnel
Last active December 10, 2021 02:27
Show Gist options
  • Save wrouesnel/4517f5088a2ac84db5dd6a40191610e2 to your computer and use it in GitHub Desktop.
Save wrouesnel/4517f5088a2ac84db5dd6a40191610e2 to your computer and use it in GitHub Desktop.
Easy bash "atexit" shutdown hooks.
#!/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
}
@wrouesnel
Copy link
Author

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment