Wednesday, February 24, 2021

Show HN: Alert yourself after a long-running task in terminal

Instantly share code, notes, and snippets.

Alert yourself after a long-running task in terminal

Alert yourself after a long-running task in terminal

I often find myself in a situation when I run a long task in terminal and if it takes more than a few seconds then I get distracted. 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

See the instructions below:

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

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.


from Hacker News https://ift.tt/3sogovH

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.