Skip to content

Instantly share code, notes, and snippets.

@scotticles
Last active May 3, 2020 16:58
Show Gist options
  • Save scotticles/99135261a133deb7f66cb2cac0d71a00 to your computer and use it in GitHub Desktop.
Save scotticles/99135261a133deb7f66cb2cac0d71a00 to your computer and use it in GitHub Desktop.
A simple linux cli timer that alerts with sound and notification. ./timer.sh -m <minutes or -s <seconds> or by default if nothing is passed it will do 30 minutes.
#!/bin/bash
# ./timer.sh -h 1
# ./timer.sh -m 30
# ./timer.sh -s 10 for 10 seconds
# ./timer.sh -e 1h10m30s
# ./timer.sh -m <message>
seconds=1800;
message="Times Up!!!!"
#checks for -s or -m else defaults to 30 minutes
if [ -n "$1" ]; then
if [ $1 == '-s' ]; then
seconds=$2
fi
if [ $1 == '-m' ]; then
seconds=$2*60
fi
if [ $1 == '-h' ]; then
seconds=$2*3600
fi
if [ $1 == '-e' ]; then
[[ $2 =~ ([0-9]+h) ]] && hours="${BASH_REMATCH[1]}"
[[ $2 =~ ([0-9]+m) ]] && minutes="${BASH_REMATCH[1]}"
[[ $2 =~ ([0-9]+s) ]] && secondsnew="${BASH_REMATCH[1]}"
hours="${hours//h/$''}"
if [ -z "$hours" ]; then
hours=0
fi
minutes="${minutes//m/$''}"
if [ -z "$minutes" ]; then
minutes=0
fi
seconds="${secondsnew//s/$''}"
if [ -z "$seconds" ]; then
seconds=0
fi
hours=$((3600*$hours))
minutes=$((60*$minutes))
seconds=$(($seconds+$minutes+$hours))
fi
fi
if [ -n "$3" ]; then
if [ $3 == '-m' ]; then
message="$4"
fi
fi
date1=$((`date +%s` + $seconds));\
while [ "$date1" -ge `date +%s` ]; do echo -ne "$(date -u --date @$(($date1 - `date +%s` )) +%H:%M:%S)\r"; done; notify-send -u critical $message && paplay /usr/share/sounds/ubuntu/ringtones/Alarm\ clock.ogg
@ryevortex
Copy link

Adding a "sleep 1" after echoing counter will reduce CPU usage.

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