blog.morishin.me

Notify the Notification Center when a long-running zsh command has finished

This article is translated from Japanese using AI

This will notify the Notification Center only when a command execution takes more than 5 seconds.

image

Required#

Install terminal-notifier

brew install terminal-notifier

Source#

Add the following to .zshrc in preexec and precmd

function preexec () {
   _prev_cmd_start_time=$SECONDS
   _cmd_is_running=true
}

function precmd() {
  if $_cmd_is_running ; then
    _prev_cmd_exec_time=$((SECONDS - _prev_cmd_start_time))
    if ((_prev_cmd_exec_time > 5)); then
      terminal-notifier -message "Command execution finished"
    fi
  fi
  _cmd_is_running=false
}

Since terminal-notifier can activate an app when the notification is clicked by passing the app's Bundle ID to the -activate option, it is convenient to set it up as follows.

terminal-notifier -message "Command execution finished" -activate com.apple.Terminal # For iTerm2, use com.googlecode.iterm2