For the last few months I've been working on pyroscope, I’ve often found myself getting distracted when I run any long tasks in the terminal (tests / docker builds).
This little snippet helps me get back to what I was doing in the terminal by alerting me via a sound message and also a macOS notification.
brew install terminal-notifier
# Open ~/.bash_profile and add these functions:
function timer_start {
timer=${timer:-$SECONDS}
}
# this makes bash run timer_start every time you run a command
trap 'timer_start' DEBUG
function notify_when_done {
timer_show=$(($SECONDS - $timer))
unset timer
# 10 is the notification threshold in seconds
if (( ${timer_show} > 10 )); then
# modify this section to fit your needs:
say "Done with task"
terminal-notifier -title "Terminal" -message "Done with task! Exit status: $? Time: ${timer_show}s"
fi
}
# this makes bash run notify_when_done command after every command
if [ "$PROMPT_COMMAND" == "" ]; then
PROMPT_COMMAND="notify_when_done"
else
PROMPT_COMMAND="$PROMPT_COMMAND; notify_when_done"
fi
Update: for information on how to set this up on Linux or with other shells (fish / zsh) see Hacker News comments.
@figbux It'll alert for those. I'm not sure what an elegant workaround would look like (is it possible to detect whether the command was interactive or not?), but you could of course do something like this (untested):