Skip to content

Instantly share code, notes, and snippets.

@moonexpr
Last active October 17, 2022 17:23
Show Gist options
  • Save moonexpr/74d9e2488f33c636a438b2f781cf1748 to your computer and use it in GitHub Desktop.
Save moonexpr/74d9e2488f33c636a438b2f781cf1748 to your computer and use it in GitHub Desktop.
bash_diligence <program>: Allows you to run a program in the background while telling you if it fails.
#!/bin/bash
# notify: Instantiates a new push notification.
# $1 string title
# $2 string body
notify()
{
NOTIFY_OPTS="-a $0 -i utilities-terminal -t 8000"
notify-send ${NOTIFY_OPTS} "$1" "$2"
}
if [[ $# < 1 ]]; then
echo "Usage: bash_diligence <program> {args}"
exit 1
fi
ERR=$({ $1 ${@:2} 1>- 2>&3; } 3>&1)
# res: exit code 0, happy ending>
EXIT_CODE=$?
[[ $EXIT_CODE = 0 ]] && exit 0
# res: exit code 0<, err ending.
SCRIPT_NAME=$(basename $1)
SCRIPT_NAME=${SCRIPT_NAME^}
if [[ "$ERR" ]]; then
notify "$SCRIPT_NAME: Runtime Error" "$ERR"
else
notify "$SCRIPT_NAME: Runtime Anomaly" "Script exited with #$EXIT_CODE. There is no error message available."
fi
@moonexpr
Copy link
Author

image

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