Skip to content

Instantly share code, notes, and snippets.

@elliot-u410
Last active October 15, 2021 20:46
Show Gist options
  • Save elliot-u410/ec65d22b4524a86e2373ecfaa1bcb512 to your computer and use it in GitHub Desktop.
Save elliot-u410/ec65d22b4524a86e2373ecfaa1bcb512 to your computer and use it in GitHub Desktop.
alertcmd.sh
#!/usr/bin/env bash
set -uo pipefail
: "${ALERT_CMD_THRESHOLD_SECONDS:=5}"
: "${ALERT_CMD_SUCCESS_SOUND:=glass}"
: "${ALERT_CMD_FAIL_SOUND:=sosumi}"
if ! which terminal-notifier > /dev/null; then
>&2 echo "Install terminal-notifier!"
exit 1
fi
start=$(date +%s)
"$@"
exitcode="$?"
end=$(date +%s)
runtime=$((end-start))
if (( runtime > ALERT_CMD_THRESHOLD_SECONDS )); then
success="failed"
sound="$ALERT_CMD_FAIL_SOUND"
if [ "$exitcode" = "0" ]; then
success="succeeded"
sound="$ALERT_CMD_SUCCESS_SOUND"
fi
terminal-notifier -title "Command $success in ${runtime}s" -message "$*" -sound "$sound"
fi
exit "$exitcode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment