Skip to content

Instantly share code, notes, and snippets.

@ilukashin
Last active June 7, 2024 08:07
Show Gist options
  • Select an option

  • Save ilukashin/6cbd6f3e531c6a9ab69f49207714b791 to your computer and use it in GitHub Desktop.

Select an option

Save ilukashin/6cbd6f3e531c6a9ab69f49207714b791 to your computer and use it in GitHub Desktop.
MacOS zsh terminal notification hooks for long executed operations
# here i am using terminal-notifier
# https://github.com/julienXX/terminal-notifier
# you can use your own solution
# main setting stored in variable
# local execution_treshold=30
# if your process executed more than 30 seconds
# you will get notified
function sysnotify() {
terminal-notifier -group 'background' -title 'Background process' -message '✅' -subtitle 'finished' -sound glass
}
# definition of zsh hook
function preexec() {
cmd_execution_begin_time=$(date +%s)
}
# definition of zsh hook
function precmd() {
# alternative to cmd_execution_end_time
local cmd_new_prompt_time=$(date +%s)
# seconds
local execution_treshold=30
# check time exists for cmd initialization
if [[ -v cmd_execution_begin_time ]]; then
else
cmd_execution_begin_time=cmd_new_prompt_time
fi
local last_execution_time=$((cmd_new_prompt_time - cmd_execution_begin_time))
if (($last_execution_time > $execution_treshold))
then
sysnotify
fi
}
# i prefer to store configs in separated files
# so add this line in your .zshrc
source ~/.hooksrc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment