Command Line – Command to Monitor Elapsed Time in Background

command linetime

I've been trying to find a decent way to monitor elapsed time in the shell. Basically, I'm asking for a stopwatch. The end result is easy enough to accomplish; I've seen examples of using time read, and then hitting enter when you're done, but that method makes the current terminal unusable.

Ideally, I'd like something like:

timer start
(do some work, vim, python, etc.)
timer stop

It also would be nice if there was a way to pause the timer. For reference, I was planning on using it to time how long it takes to finish programming assignments from MIT's OCW. As such, since the assignments might be completed in more than one sitting, a way to pause and continue the timer would be beneficial.

Is there anything like this?

Best Answer

You may try a shell script (call it timer.sh) like this:

. timer.val

case "$1" in 
  start)
   echo $TIMERSTART
   echo "TIMERSTART=`date +%s`" > timer.val
   echo $TIMERSTART
   ;;
  stop)
   echo $TIMERSTART
   TIMEREND=`date +%s`
   echo $TIMEREND
   let RESULT=$TIMEREND-$TIMERSTART
   echo $RESULT
   ;;
  *)
   echo "no command"
esac