Skip to content

Instantly share code, notes, and snippets.

@petethepig
Last active May 6, 2025 05:45
Show Gist options
  • Select an option

  • Save petethepig/2d29e8b7e2ebc808bfe760b632608966 to your computer and use it in GitHub Desktop.

Select an option

Save petethepig/2d29e8b7e2ebc808bfe760b632608966 to your computer and use it in GitHub Desktop.
Alert yourself after a long-running task in terminal

Alert yourself after a long-running task in terminal

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.

demo

Instructions

Step 1. Install terminal-notifier:

brew install terminal-notifier

Step 2. Add the following code to .bash_profile:

# 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.

@mihirkhandekar
Copy link
Copy Markdown

Thanks for the gist. I created (Telert) to easily configure sound alerts, desktop popup, or even Telegram/Slack/Teams/Discord messages when a long-running command finishes. It also works in Python commands.

Setting it up is quick and easy

pip install telert
telert config audio --set-default  # for sound
telert config desktop --set-default  # for popup
telert config telegram --token "<token>" --chat-id "<chat-id>" --set-default # for telegram

You can then use it with your long running commands in your script.

long_running_command | telert # Option 1
telert run long_running_Command # Option 2

Telert will time the command and send a notification to the provider channel when it completes with the execution time and exit code.

https://github.com/navig-me/telert/

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