Re: unix tool as precise counter/timer for periodic print/exec
Hi Karl,
> Perhaps the sleepenh package will help you?
>
> Not a solution per se, but possibly a useful building block..
It works perfectly and was exactly what I was looking for! Thanks a lot!
In case anybody ever finds this thread later on, this is what I now
wrote based on sleepenh which does exactly what I was looking for in my
initial email:
--%<---------------------------------------------------------------------------
#!/bin/sh
usage() {
echo "Usage: $0 [ARGS] [COMMAND]"
echo
echo "It counts upward (default) or downward (-d) starting at START (-s)"
echo "with a configurable floating point interval of SECS seconds (-n,"
echo "default 1.0) until infinity (default) or up to a maximum number of"
echo "COUNT intervals (-c). It optionally prints this counter (-p) and/or"
echo "the current date (-t) at each interval. It optionally executes a"
echo "COMMAND per interval which it can also fork (-f) in case the command"
echo "is expected to take longer than SECS seconds."
echo
echo " -f fork COMMAND"
echo " -p print counter"
echo " -d count downward (default: upward)"
echo " -n SECS interval of SECS (can be floating point)"
echo " -c COUNT only run for COUNT interval(s)"
echo " -s START start counting at START"
echo " -t print the output of date(1) every interval"
echo " -h print this help message"
}
INTERVAL=1.0
MAX_COUNT=-1
DOWN=0
PRINT=0
FORK=0
PRINT_DATE=0
START=0
while getopts fpdn:c:s:th option; do
case $option in
f)
FORK=1
;;
p)
PRINT=1
;;
d)
DOWN=1
;;
n)
INTERVAL="$OPTARG"
;;
c)
MAX_COUNT="$OPTARG"
;;
s)
START="$OPTARG"
;;
t)
PRINT_DATE=1
;;
h)
usage
exit 0
;;
[?])
usage 1>&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
TIMESTAMP=`sleepenh 0`
I=$START
while true; do
if [ $# -ne 0 ]; then
if [ $FORK -eq 1 ]; then
$@ &
else
$@
fi
fi
if [ $PRINT -eq 1 ]; then
echo $I
if [ $DOWN -eq 0 ]; then
I=$((I+1))
else
I=$((I-1))
fi
fi
if [ $PRINT_DATE -eq 1 ]; then
date
fi
if [ $DOWN -eq 0 ]; then
[ $((I-START)) -gt $MAX_COUNT ] && break
else
[ $((START-I)) -gt $MAX_COUNT ] && break
fi
TIMESTAMP=`sleepenh $TIMESTAMP $INTERVAL`; done
--%<---------------------------------------------------------------------------
Feel free to use the above in any way you want.
cheers, josch
Reply to: