How can I display a real-time countdown timer on the Linux terminal? Is there an existing app or, even better, a one liner to do this?
129 Answers
I'm not sure why you need beep. If all you want is a stopwatch, you can do this:
while true; do printf '%s\r' "$(date)"; doneThat will show you the seconds passing in realtime and you can stop it with Ctrl+C. If you need greater precision, you can use this to give you nanoseconds:
while true; do printf '%s\r' "$(date +%H:%M:%S:%N)"; doneFinally, if you really, really want "stopwatch format", where everything starts at 0 and starts growing, you could do something like this:
start=$(date +%s)
while true; do time="$(($(date +%s) - $start))" printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
doneFor a countdown timer (which is not what your original question asked for) you could do this (change seconds accordingly):
seconds=20
start="$(($(date +%s) + $seconds))"
while [ "$start" -ge `date +%s` ]; do time="$(( $start - `date +%s` ))" printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)"
doneYou can combine these into simple commands by using bash (or whichever shell you prefer) functions.
In bash, add these lines to your ~/.bashrc (the sleep 0.1 will make the system wait for 1/10th of a second between each run so you don't spam your CPU):
countdown() { start="$(( $(date '+%s') + $1))" while [ $start -ge $(date +%s) ]; do time="$(( $start - $(date +%s) ))" printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" sleep 0.1 done
}
stopwatch() { start=$(date +%s) while true; do time="$(( $(date +%s) - $start))" printf '%s\r' "$(date -u -d "@$time" +%H:%M:%S)" sleep 0.1 done
}You can then start a countdown timer of one minute by running:
countdown 60You can countdown two hours with:
countdown "$((2 * 60 * 60))"or a whole day using:
countdown "$((24 * 60 * 60))"And start the stopwatch by running:
stopwatchIf you need to be able to deal with days as well as hours, minutes and seconds, you could do something like this:
countdown() { start="$(( $(date +%s) + $1))" while [ "$start" -ge $(date +%s) ]; do ## Is this more than 24h away? days="$(($(($(( $start - $(date +%s) )) * 1 )) / 86400))" time="$(( $start - `date +%s` ))" printf '%s day(s) and %s\r' "$days" "$(date -u -d "@$time" +%H:%M:%S)" sleep 0.1 done
}
stopwatch() { start=$(date +%s) while true; do days="$(($(( $(date +%s) - $start )) / 86400))" time="$(( $(date +%s) - $start ))" printf '%s day(s) and %s\r' "$days" "$(date -u -d "@$time" +%H:%M:%S)" sleep 0.1 done
}Note that the stopwatch function hasn't been tested for days since I didn't really want to wait 24 hours for it. It should work, but please let me know if it doesn't.
My favorite way is:
Start:
time catStop:
ctrl+cAs @wjandrea commented below, another version is to run:
time readand press Enter to stop
I was looking for the same thing and ended up writing something more elaborate in Python:
This will give you a simple 10-second countdown:
sudo pip install termdown
termdown 10Source:
5Use leave (it at least works on BSD descendants):
man leaveSet a timer for 15 minutes:
leave +0015
Alarm set for Thu Nov 3 14:19:31 CDT 2016. (pid 94317) 3 I've used this one:
countdown()
( IFS=: set -- $* secs=$(( ${1#0} * 3600 + ${2#0} * 60 + ${3#0} )) while [ $secs -gt 0 ] do sleep 1 & printf "\r%02d:%02d:%02d" $((secs/3600)) $(( (secs/60)%60)) $((secs%60)) secs=$(( $secs - 1 )) wait done echo
)Example:
countdown "00:07:55"Here's a source.
4Short answer:
for i in `seq 60 -1 1` ; do echo -ne "\r$i " ; sleep 1 ; doneExplanation:
I know there are a lot of answers, but I just want to post something very close to OP's question, that personally I would accept as indeed "oneliner countdown in terminal". My goals were:
- One liner.
- Countdown.
- Easy to remember and type in console (no functions and heavy logic, bash only).
- Does not require additional software to install (can be used on any server I go via ssh, even if I do not have root there).
How it works:
seqprints numbers from 60 to 1.echo -ne "\r$i "returns caret to beginning of the string and prints current$ivalue. Space after it is required to overwrite previous value, if it was longer by characters than current$i(10 -> 9).
I use this small Go program:
package main
import ( "fmt" "time"
)
func format(d time.Duration) string { mil := d.Milliseconds() % 1000 sec := int(d.Seconds()) % 60 min := int(d.Minutes()) return fmt.Sprintf("%v m %02v s %03v ms", min, sec, mil)
}
func main() { t := time.Now() for { time.Sleep(10 * time.Millisecond) s := format(time.Since(t)) fmt.Print("\r", s) }
} 0 Another approach
countdown=60 now=$(date +%s) watch -tpn1 echo '$((now-$(date +%s)+countdown))'For Mac:
countdown=60 now=$(date +%s) watch -tn1 echo '$((now-$(date +%s)+countdown))'
#no p option on mac for watchIf one wants a signal when it hits zero, one could e.g. build it with a command that returned a non-zero exit status at zero and combine it with watch -b, or something, but if one wants to build a more elaborate script, this is probably not the way to go; it is more of a "quick and dirty one-liner" type solution.
I like the watch program in general. I first saw it after I had already written countless while sleep 5; do loops to different effects. watch was demonstrably nicer.
I have combined terdon's very good answer, into a function which at the same time displays the time since the start, and the time till the end. There are also three variants, so it's easier to call (you don't have to do Bash math), and it's also abstracted.
Example of use:
{ ~ } » time_minutes 15
Counting to 15 minutes
Start at 11:55:34 Will finish at 12:10:34 Since start: 00:00:08 Till end: 00:14:51And something like a work timer:
{ ~ } » time_hours 8
Counting to 8 hours
Start at 11:59:35 Will finish at 19:59:35 Since start: 00:32:41 Till end: 07:27:19And if you need some very specific time:
{ ~ } » time_flexible 3:23:00
Counting to 3:23:00 hours
Start at 12:35:11 Will finish at 15:58:11 Since start: 00:00:14 Till end: 03:22:46Here's the code to put into your .bashrc
function time_func()
{ date2=$((`date +%s` + $1)); date1=`date +%s`; date_finish="$(date --date @$(($date2)) +%T )" echo "Start at `date +%T` Will finish at $date_finish" while [ "$date2" -ne `date +%s` ]; do echo -ne " Since start: $(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S) Till end: $(date -u --date @$(($date2 - `date +%s`)) +%H:%M:%S)\r"; sleep 1 done printf "\nTimer finished!\n" play_sound ~/finished.wav
}
function time_seconds()
{ echo "Counting to $1 seconds" time_func $1
}
function time_minutes()
{ echo "Counting to $1 minutes" time_func $1*60
}
function time_hours()
{ echo "Counting to $1 hours" time_func $1*60*60
}
function time_flexible() # Accepts flexible input hh:mm:ss
{ echo "Counting to $1" secs=$(time2seconds $1) time_func $secs
}
function play_sound() # Adjust to your system
{ cat $1 > /dev/dsp
}
function time2seconds() # Changes hh:mm:ss to seconds. Found in some other Stack Exchange answer
{ a=( ${1//:/ }) echo $((${a[0]}*3600+${a[1]}*60+${a[2]}))
}Combine this with some way of playing sound in linux terminal (Play MP3 or WAV file via the Linux command line) or Cygwin (cat /path/foo.wav > /dev/dsp works for me in Babun/Windows 7) and you have a simple flexible timer with alarm!
I'm surprised that nobody used the sleepenh tool in their scripts. Instead, the proposed solutions either use a sleep 1 between subsequent timer outputs or a busy loop that outputs as fast as possible. The former is inadequate because due to the small time spent doing the printing, the output will not actually happen once per second but a bit less than that which is suboptimal. After enough time passed, the counter will skip a second. The latter is inadequate because it keeps the CPU busy for no good reason.
The tool that I have in my $PATH looks like this:
#!/bin/sh
if [ $# -eq 0 ]; then TIMESTAMP=$(sleepenh 0) before=$(date +%s) while true; do diff=$(($(date +%s) - before)) printf "%02d:%02d:%02d\r" $((diff/3600)) $(((diff%3600)/60)) $((diff%60)) TIMESTAMP=$(sleepenh $TIMESTAMP 1.0); done exit 1 # this should never be reached
fi
echo "counting up to $@"
"$0" &
counterpid=$!
trap "exit" INT TERM
trap "kill 0" EXIT
sleep "$@"
kill $counterpidThe script can either be used as a stop watch (counting up until interrupted) or as a timer that runs for the specified amount of time. Since the sleep command is used, this script allows to specify the duration for which to count in the same precision as your sleep allows. On Debian and derivatives, this includes sub-second sleeps and a nice human-readable way to specify the time. So for example you can say:
$ time countdown 2m 4.6s
countdown 2m 4.6s 0.00s user 0.00s system 0% cpu 2:04.60 totalAnd as you can see, the command ran exactly for 2 minutes and 4.6 seconds without much magic in the script itself.
EDIT:
The sleepenh tool comes from the package of the same name in Debian and its derivatives like Ubuntu. For distributions that don't have it, it comes from
The advantage of sleepenh is, that it is able to take into account the small delay that accumulates over time from the processing of other things than the sleep during a loop. Even if one would just sleep 1 in a loop 10 times, the overall execution would take a bit more than 10 seconds because of the small overhead that comes from executing sleep and iterating the loop. This error slowly accumulates and would over time make our stopwatch timer more and more imprecise. To fix this problem, one must each loop iteration compute the precise time to sleep which is usually slightly less than a second (for one second interval timers). The sleepenh tool does this for you.
sw is a simple stopwatch that will run forever.
Install
wget -q -O - | sh -s -- -u Usage
sw - start a stopwatch from 0, save start time in ~/.sw
sw [-r|--resume] - start a stopwatch from the last saved start time (or current time if no last saved start time exists) - "-r" stands for --resume 3 I ended up writing my own shell script: GitHub gist
#!/bin/sh
# script to create timer in terminal
# Jason Atwood
# 2013/6/22
# Start up
echo "starting timer script ..."
sleep 1 # seconds
# Get input from user
read -p "Timer for how many minutes?" -e DURATION
DURATION=$(( $DURATION*60 )) # convert minutes to seconds
# Get start time
START=$(date +%s)
# Infinite loop
while [ -1 ]; do
clear # Clear window
# Do math
NOW=$(date +%s) # Get time now in seconds
DIF=$(( $NOW-$START )) # Compute diff in seconds
ELAPSE=$(( $DURATION-$DIF )) # Compute elapsed time in seconds
MINS=$(( $ELAPSE/60 )) # Convert to minutes... (dumps remainder from division)
SECS=$(( $ELAPSE - ($MINS*60) )) # ... and seconds
# Conditional
if [ $MINS == 0 ] && [ $SECS == 0 ] # if mins = 0 and secs = 0 (i.e. if time expired)
then # Blink screen
for i in `seq 1 180`; # for i = 1:180 (i.e. 180 seconds)
do
clear # Flash on
setterm -term linux -back red -fore white # use setterm to change background color
echo "00:00 " # extra tabs for visibility
sleep 0.5
clear # Flash off
setterm -term linux -default # Clear setterm changes from above
echo "00:00" # (I.e. go back to white text on black background)
sleep 0.5
done # End for loop
break # End script
else # Else, time is not expired
echo "$MINS:$SECS" # Display time
sleep 1 # Sleep 1 second
fi # End if
done # End while loop 2 Take a look at TermTime. It's a nice terminal based clock and stopwatch:
pip install termtime For future reference, there is a command line tool called µTimer with very straightforward command line options for a countdown/count-up timer.
Simply use watch + date in UTC time. You can also install some package for big display...
export now="`date +%s -u`";
watch -n 0,1 'date +%T -u -d @$((`date +%s` - $now ))'
#Big plain characters
watch -n 0,1 'date +%T -u -d @$((`date +%s` - $now )) | toilet -f mono12'
#Big empty charaters
watch -n 0,1 'date +%T -u -d @$((`date +%s` - $now )) | figlet -c -f big'Try it!
See also
A Python example:
#!/usr/bin/python
def stopwatch ( atom = .01 ): import time, sys, math start = time.time() last = start sleep = atom/2 fmt = "\r%%.%sfs" % (int(abs(round(math.log(atom,10)))) if atom<1 else "") while True: curr = time.time() subatom = (curr-last) if subatom>atom: # sys.stdout.write( "\r%.2fs" % (curr-start)) sys.stdout.write( fmt % (curr-start)) sys.stdout.flush() last = curr else: time.sleep(atom-subatom)
stopwatch() Pretend you are a person on OS X looking for a command line stopwatch. Pretend that you don't want to install the gnu tools and just want to run with the Unix date.
In that case do as terdon says, but with this modification:
function stopwatch(){ date1=`date +%s`; while true; do echo -ne "$(date -jf "%s" $((`date +%s` - $date1)) +%H:%M:%S)\r"; sleep 0.1 done
} 2 just a one liner
N=100; while [[ $((--N)) > 0 ]]; do echo $N | figlet -c && sleep 1 ; donescreenhost
Also we can clear the screen ( Terminal ) using ANSI Escape sequences using 2J format.
N=100; while [[ $((--N)) > 0 ]]; do echo -e "\033[2J\033[0m"; echo "$N" | figlet -c && sleep 1 ; doneNOTE
installing figlet command is required if you need in BIG font, otherwise remove figlet part.
N=100; while [[ $((--N)) > 0 ]]; do echo $N && sleep 1 ; doneand you can make to have a beautiful output using lolcat ...
N=100; while [[ $((--N)) > 0 ]]; do echo "$N" | figlet -c | lolcat && sleep 1 ; donescreenhsot
If your system already has Ruby, you can use:
#!/usr/bin/env ruby
n = ARGV[0].to_f
go_beginning_of_line = "\033[G"
clear_till_end_of_line = "\033[K"
time_end = Time.now + n
while Time.now < time_end more = time_end - Time.now print "#{go_beginning_of_line}Counting down #{more.round(1)} of #{n} seconds#{clear_till_end_of_line}" sleep [0.1, more].min
end
puts "#{go_beginning_of_line}Counting down #{n} seconds. \a\aDone.#{clear_till_end_of_line}" This is a terminal based timer:
Simple one liner to do the countdown job:
cnt=10;until [ $cnt -eq 0 ]; do printf "\rYour Quiz will start in $cnt seconds.... ";sleep 1;cnt=$(expr $cnt - 1);done;echo;This one liner is taken from my open source project called Automated_Quiz, which is hosted here :
For Stopwatch :
Paste the following three lines one by one on the terminal and press enter. Press Ctrl+c on the second line to exit the stopwatch, as required.
function stopwatch() { cntd=$1; printf '%dd:%dh:%dm:%ds\n' $((cntd/86400)) $((cntd%86400/3600)) $((cntd%3600/60)) $((cntd%60)) ; };
cnt=0;while true; do printf "\rStopwatch : $cnt seconds";export var=$cnt;sleep 1;cnt=$(expr $cnt + 1);done;echo;
stopwatch $var 1 Found this question earlier today, when looking for a term application to display a large countdown timer for a workshop. None of the suggestions was exactly what I needed, so I quickly put another one together in Go:
As the question is already sufficiently answered, consider this to be for the sake of posterity.
$ sleep 1500 && xterm -fg yellow -g 240x80 &
When that big terminal with yellow text jumps up, time to get up and stretch!
Notes: - 1500 seconds = 25 minute pomodoro - 240x80 = terminal size with 240 character row, and 80 rows. Fills up a screen for me noticeably.
Credit:
1A GUI version of the stopwatch
date1=`date +%s`
date1_f=`date +%H:%M:%S____%d/%m`
( while true; do date2=$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S) echo "# started at $date1_f \n$date2" done
) |
zenity --progress \ --title="Stop Watch" \ --text="Stop Watch..." \ --percentage=0 This is similar to the accepted answer, but terdon's countdown() gave me syntax errors. This one works great for me, though:
function timer() { case "$1" in -s) shift;; *) set $(($1 * 60));; esac; local S=" "; for i in $(seq "$1" -1 1); do echo -ne "$S\r $i\r"; sleep 1; done; echo -e "$S\rTime's up!"; }You can put it in .bashrc and then execute with: timer t (where t is time in minutes).
If you have pv installed, you can use the following one-liner to display a countdown timer and sleep for a minute (or any other amount of time, just change 60 to the desired number of seconds):
cat /dev/zero | pv -B 1 -L 1 -tpe -s 60 -S > /dev/nullYou can also put it in a function:
sleep_with_progress() { cat /dev/zero | pv -B 1 -L 1 -tpe -s "$1" -S > /dev/null
}
# If you prefer a simpler output with just the countdown:
sleep_with_countdown() { cat /dev/zero | pv -B 1 -L 1 -e -s "$1" -S > /dev/null
}Sample outputs taken at 5 seconds after starting:
user@host:~ $ sleep_with_progress 60
0:00:05 [===> ] 8% ETA 0:00:55user@host:~ $ sleep_with_countdown 60
ETA 0:00:55Explanation:
cat /dev/zero produces an infinite amount of ASCII zero (\0) characters. pv displays progress, rate limits the data flowing through it and terminates after 60 characters (details below). Finally, the redirection to /dev/null makes sure that the \0 characters are not sent to the terminal.
The parameters used for pv are:
-B 1sets the buffer size to 1.-L 1rate limits the pipe to 1 character per second.-tpeturns on the display of the time, progress and ETA indicators (while-eonly shows the latter).-s 60specifies thatpvshould expect 60 bytes.-Stellspvto stop after reaching the specified size even though the input continues (it is infinite).
Another option under the "existing app" category: peaclock. Displays a "large" stopwatch, timer, or clock in the terminal. Quite a few key bindings facilitate interactive use.
A slightly shorter Python (3) example:
import sys
import time
from datetime import datetime
from datetime import timedelta
start = time.time() * 1000
while True: try: now = time.time() * 1000 elapsed = now - start out = str(timedelta(milliseconds=elapsed)) print(out + "\033[?25l", end='\r') except KeyboardInterrupt: print(out + '\033[?25h', end='\n') sys.exit(0)Hides / restores blinking cursor. Control-c to quit.
If you would like a compilable program for whatever reason, the following would work:
#include <iostream>
#include <string>
#include <chrono>
int timer(seconds count) { auto t1 = high_resolution_clock::now(); auto t2 = t1+count; while ( t2 > high_resolution_clock::now()) { std::cout << "Seconds Left:" << std::endl << duration_cast<duration<double>>(count-(high_resolution_clock::now()-t1)).count() << std::endl << "\033[2A\033[K"; std::this_thread::sleep_for(milliseconds(100)); } std::cout << "Finished" << std::endl; return 0;
}This can be used in other programs as well and easily ported, if a Bash environment isn't available or you just prefer using a compiled program.
0