7
Mar

Simple Stopwatch Script

The following is a short and plain shell script that will start a timer when you run the program that counts up. I think you could argue that it’s not a stopwatch because it doesn’t support laps, but it’s close enough for me. You can easy get started by copying the following code block into a text editor, saving as stopwatch.sh, running chmod +x stopwatch.sh to make it executable, and finally starting it with ./stopwatch.sh. To stop it, hit Ctrl+c.

#!/bin/bash

BEGIN=$(date +%s)

echo Starting Stopwatch...

while true; do
   NOW=$(date +%s)
   let DIFF=$(($NOW - $BEGIN))
   let MINS=$(($DIFF / 60))
   let SECS=$(($DIFF % 60))
   let HOURS=$(($DIFF / 3600))
   let DAYS=$(($DIFF / 86400))

   # \r  is a "carriage return" - returns cursor to start of line
   printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS
   sleep 0.25
done

The previous script will allow you to track from the time you say “go” until you stop it. It’s also nice to the real-estate on your terminal and will use backspace to remove the characters printed from before to make room for the new ones.

If you’re not worried about starting it “now” or real estate in the terminal, you could always use uptime and throw it into a while loop like this:

#!/bin/sh

while true; do uptime | cut -d' ' -f2; done

Both are simple, both have their own advantages and disadvantages. Choose wisely. 😉

Source: unix.com forums

I used the scripts to help me figure out how long it was taking my desktop to lock up so I could troubleshoot it better. Another use might be to keep track of how much time you’re spending on the computer vs the amount of time spent skiing! Have fun either way.

Edit
A subscriber, Jim, came up with a much better stopwatch script than my thrown together example. Jim sent it via email, but I’ll post it here for all to see.

It doesn’t start counting till you press the spacebar, pressing the
spacebar again pauses it counting, until the spacebar is pressed to
continue counting.

Press ‘q’ to quit
Press ‘r’ to reset to zero


#!/bin/bash

# sets stdin to no echo and give a char every tenth of a sec.
stty -echo -icanon time 1 <&0 chkspace () { if ! read -t 0 ; then return 1 ; fi # no char pressed read -n 1 ans if [ "$ans" = " " ]; then return 0 ; fi case "$ans" in r|R) COUNT=0 ; BEGIN=$(date +%s) printf "\r%3d Days, %02d:%02d:%02d" 0 0 0 0 ;; q|Q) stty echo icanon <&0 echo "" exit 0 ;; [1-9]) echo " - $ans" ;; esac return 1 } echo "Stopwatch: to start and stop press the SPACEBAR..." printf "\r%3d Days, %02d:%02d:%02d" 0 0 0 0 COUNT=0 IFS= while true ; do while true; do if chkspace ; then break; fi sleep 0.1 done BEGIN=$(date +%s) while true; do NOW=$(date +%s) let DIFF=$(($NOW - $BEGIN + $COUNT)) let MINS=$(($DIFF / 60)) let SECS=$(($DIFF % 60)) let HOURS=$(($DIFF / 3600)) let DAYS=$(($DIFF / 86400)) # \r is a "carriage return" - returns cursor to start of line printf "\r%3d Days, %02d:%02d:%02d" $DAYS $HOURS $MINS $SECS if chkspace ; then break; fi sleep 0.1 done COUNT=$DIFF done [/bash]

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 4 Comments So Far

  • pluggi
    March 8th, 2012 at 4:13 am

    What about the command time? http://unixhelp.ed.ac.uk/CGI/man-cgi?time

  • [email protected]
    March 8th, 2012 at 8:01 am

    time could work too, but it doesn’t display how much time has passed. If that’s not a big deal, then just use time cat and press Ctrl+C to exit.

  • loomsen
    June 6th, 2012 at 8:33 am

    You can simply set SECONDS=0 in bash, and it will start counting the seconds.
    See:

    [email protected]:~$ SECONDS=0; while ((SECONDS<3)); do echo $SECONDS; sleep 1; done
    0
    1
    2

  • Joe Fieger
    January 27th, 2017 at 3:11 pm

    There’s 2 bugs in the code…

    You need to add mod 60 to minutes:
    let MINS=$(($DIFF / 60 % 60))

    And mod 24 to hours:
    let HOURS=$(($DIFF / 3600 % 24))

    Otherwise minutes can be greater than 60 and house greater than 24. Great tool though, I use it all the time.

Share your thoughts, leave a comment!