After trying to find a similar command in bash to get this done, I finally just wrote a bash script...
I needed a timer that I could start, and then check periodically.
This one will work across reboot (if /tmp cleared on boot, change the tmp dir).
This one should make it into C, and then into coreutils IMO.
Also, I left room in the parser (MAIN) for growth. Feel free to add/modify.
#!/bin/bash
#########################################################################
# -----stopwatch----- #
# Written by: Josh Dotson (
[email protected]) (12-03-08) #
# Originaly written for private use #
# License: GNU GPL v2, or later at your option #
# Versioning is the latest modification date (YYMMDD). #
# #
# Modified by: #
# Josh Dotson (12-03-09) added header and versioning #
# (if you modify this, put your name and the date here) #
# #
# Synopsys: #
# Simple timer, written in bash. Output for use by other scripts. #
# #
#########################################################################
## FUNCTION LIST ########################################################
# This just gets the version number out of the header of this file.
function get_ver {
ver_p=(000000 `cat $0 | grep \# | grep -o [0-9][0-9]-[0-9][0-9]-[0-9][0-9] | sed 's/-//g' | awk '{ printf(" %s ",$1)}' | awk '{ print $0}'`)
count=$((${#ver_p[@]}-1))
if [ "$count" -gt "1" ];then
for ((a=1; a <= count ; a++));do
ifer=${ver_p[$a]}
if [ "$ifer" -gt "${ver_p[0]}" ];then
ver_p[0]=$ifer
fi
done
else
ver_p[0]=${ver_p[1]}
fi
ver=${ver_p[0]}
yr="`echo $ver | cut -c1-2`"
mth="`echo $ver | cut -c3-4`"
day="`echo $ver | cut -c5-6`"
case $day in
01)dth=st;;
21)dth=st;;
31)dth=st;;
02)dth=nd;;
22)dth=nd;;
03)dth=rd;;
23)dth=rd;;
*)dth=th;;
esac
case $mth in
01)mn=Jan;;
02)mn=Feb;;
03)mn=Mar;;
04)mn=Apr;;
05)mn=May;;
06)mn=Jun;;
07)mn=Jul;;
08)mn=Aug;;
09)mn=Sep;;
10)mn=Oct;;
11)mn=Nov;;
12)mn=Dec;;
esac
}
function p_syntax {
get_ver
echo ""
echo "`basename $0`, v$ver ($day$dth of $mn, 20$yr)"
echo ""
echo "SYNTAX:"
echo -e " `basename $0` -t:title -[start|stop|show]"
echo ""
echo "Notes:"
echo " Only times in whole seconds."
echo " Do not use spaces in the title. Whitespace WILL cause errors."
echo " A common title might be the PID of what you are timing."
echo ""
echo "ERRORS:"
echo " FE = file exists, timer already started"
echo " NF = no file, timer not started"
echo " *A non-zero exit status denotes an error.*"
echo ""
}
function start_w {
if [ -f /tmp/$title.stpw ];then
echo "FE"; exit 1
else
echo -n $(date +%s) >> /tmp/$title.stpw
fi
}
function stop_w {
if [ -f /tmp/$title.stpw ];then
BEGIN=$(cat /tmp/$title.stpw)
echo $(($NOW - $BEGIN))
rm /tmp/$title.stpw
else
echo "NF"; exit 1
fi
}
function show_w {
if [ -f /tmp/$title.stpw ];then
BEGIN=$(cat /tmp/$title.stpw)
echo $(($NOW - $BEGIN))
else
echo "NF"; exit 1
fi
}
# ### MAIN ### ### ### ### ### ### ### ### ### ### ### ###
if [ "x$1" == "x" ];then
p_syntax
else
while [ "x$1" != "x" ];do
if [ "`echo $1 | cut -d: -f1`" == "-t" ];then
title="`echo $1 | cut -d: -f2`"
shift
else
if [ "$act" != "syntax" ];then
case $1 in
-start)act=start;;
-stop)act=stop;;
-show)act=show;;
*)act=syntax;;
esac
fi
shift
fi
done
if [ "x$title" != "x" ];then
NOW=$(date +%s)
case $act in
start)start_w;;
stop)stop_w;;
show)show_w;;
*)p_syntax;;
esac
else
p_syntax
fi
fi
# ### END MAIN ### ### ### ### ### ### ### ### ### ### ###