Skip to content

Instantly share code, notes, and snippets.

@michaelvillar
Created August 25, 2016 00:19
Show Gist options
  • Save michaelvillar/ed466c6dc6ac0a8549ad4f9c75663c24 to your computer and use it in GitHub Desktop.
Save michaelvillar/ed466c6dc6ac0a8549ad4f9c75663c24 to your computer and use it in GitHub Desktop.
Get a OS X Notification when a slow CLI command finishes
# Based on http://frantic.im/notify-on-completion and https://gist.github.com/jamesmacaulay/860763
# Notify on completion
function f_notifyme {
LAST_EXIT_CODE=$?
CMD=$(fc -ln -1)
terminal-notifier -title "$CMD" -message "Status code: $LAST_EXIT_CODE" &
}
save_preexec_time() {
export PREEXEC_CMD="$(history | tail -1 | sed 's/ *[0-9]* *//')"
export PREEXEC_TIME=$(date +'%s')
}
check_long_running_command() {
exitstatus=$?
stop=$(date +'%s')
start=${PREEXEC_TIME:-$stop}
let elapsed=$stop-$start
if [ $elapsed -gt 1 ]; then
f_notifyme
fi
PREEXEC_TIME=
PREEXEC_CMD=
}
autoload -Uz add-zsh-hook
add-zsh-hook preexec save_preexec_time
add-zsh-hook precmd check_long_running_command
@michaelvillar
Copy link
Author

Prerequisites:

  • zsh
  • terminal-notifier: brew install terminal-notifier

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