FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Debian > Debian User

 
 
LinkBack Thread Tools
 
Old 05-02-2012, 08:20 AM
Johannes Schauer
 
Default unix tool as precise counter/timer for periodic print/exec

Hi,

I'm looking for a unix tool that does nothing else than increment and
print an integer with a fixed frequency. As a bonus it should be able to
execute a command with a fixed frequency. The special requirement: it
should precise in the interval.

Thus, the following will not work:

#!/bin/sh
while true; do
print_counter
sleep 1
done

because each loop iteration will take one second plus how long
print_counter takes to execute.

Even the following will not work:

#!/bin/sh
while true; do
print_counter &
sleep 1
done

Because even the time it takes to fork print_counter will accumulate
over time.

Another inconvenience of the above is, that not all sleep(1)
implementations do accept floating point arguments to run the above
faster than every second.

What I need is something that on each step recalculates how long to
sleep based on the current time so that the overall frequency remains
stable.

I do not need each print_counter to be executed exactly at a very
precise moment but just that the overall frequency stays the same. After
having it run 1000 times with a frequency of 1Hz I want 1000 seconds to
have passed.

Some libraries like glib provide functions like g_timout_add or
g_timeout_add_seconds but that does not 'catch up' as I require above.
Is there some library that does?

There is the watch(1) tool which already partly does what I want using
the --precise switch. But it runs in fullscreen and even when using
--precise, it will not compensate for commands that take longer than the
given interval length as it doesnt fork them. Also, watch(1) will not
allow intervals smaller than 0.1 seconds.

So basic requirement: print a counter (counting up or down) in a fixed
frequency. Either a tool that does that or a library that I can use to
code it.

Bonus: execute a command with a fixed frequency and in contrast to
watch(1) even when executing the command takes longer than the interval.

So is there a utility that just implements a simple, precise counter?

Is there a better version of watch(1) that is not fullscreen, allows
faster than 0.1 second intervals and forks the application so that their
runtime can exceed the interval time?

cheers, josch


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20120502082011.GA8564@hoothoot">http://lists.debian.org/20120502082011.GA8564@hoothoot
 
Old 05-02-2012, 09:23 AM
Darac Marjal
 
Default unix tool as precise counter/timer for periodic print/exec

On Wed, May 02, 2012 at 10:20:11AM +0200, Johannes Schauer wrote:
> Hi,
>
> I'm looking for a unix tool that does nothing else than increment and
> print an integer with a fixed frequency. As a bonus it should be able to
> execute a command with a fixed frequency. The special requirement: it
> should precise in the interval.

I'm not entirely sure if such a tool exists, but one thing you will need
to bear in mind is that you will need to make sure you're running a
real-time kernel (apt-cache search linux-image-rt). This will allow you
to run your look with real-time priority. If you don't have real-time
priority, then you basically have no way of ensuring that you will be
able to run print_counter at the specified time - you could always get
delayed because the kernel is writing to disk or some other process is
doing work.
 
Old 05-02-2012, 10:45 AM
Johannes Schauer
 
Default unix tool as precise counter/timer for periodic print/exec

Hi Darac,

> I'm not entirely sure if such a tool exists, but one thing you will
> need to bear in mind is that you will need to make sure you're running
> a real-time kernel (apt-cache search linux-image-rt). This will allow
> you to run your look with real-time priority. If you don't have
> real-time priority, then you basically have no way of ensuring that
> you will be able to run print_counter at the specified time - you
> could always get delayed because the kernel is writing to disk or some
> other process is doing work.

I dont require that. Let me quote my original email:

> I do not need each print_counter to be executed exactly at a very
> precise moment but just that the overall frequency stays the same.
> After having it run 1000 times with a frequency of 1Hz I want 1000
> seconds to have passed.

This is also why I made the comparison to watch(1) with the --precise
argument, which does what I want but has other deficiencies.

Thank you for your input.

cheers, josch


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20120502104526.GA19488@hoothoot">http://lists.debian.org/20120502104526.GA19488@hoothoot
 
Old 05-02-2012, 01:57 PM
"Karl E. Jorgensen"
 
Default unix tool as precise counter/timer for periodic print/exec

On Wed, May 02, 2012 at 09:20:11AM +0100, Johannes Schauer wrote:
> Hi,
>
> I'm looking for a unix tool that does nothing else than increment and
> print an integer with a fixed frequency. As a bonus it should be able to
> execute a command with a fixed frequency. The special requirement: it
> should precise in the interval.
>
> Thus, the following will not work:
>
> #!/bin/sh
> while true; do
> print_counter
> sleep 1
> done
>
> because each loop iteration will take one second plus how long
> print_counter takes to execute.

Perhaps the sleepenh package will help you?

Not a solution per se, but possibly a useful building block..


> What I need is something that on each step recalculates how long to
> sleep based on the current time so that the overall frequency remains
> stable.
>
> I do not need each print_counter to be executed exactly at a very
> precise moment but just that the overall frequency stays the same. After
> having it run 1000 times with a frequency of 1Hz I want 1000 seconds to
> have passed.
>
> Some libraries like glib provide functions like g_timout_add or
> g_timeout_add_seconds but that does not 'catch up' as I require above.
> Is there some library that does?
>
> There is the watch(1) tool which already partly does what I want using
> the --precise switch. But it runs in fullscreen and even when using
> --precise, it will not compensate for commands that take longer than the
> given interval length as it doesnt fork them. Also, watch(1) will not
> allow intervals smaller than 0.1 seconds.
>
> So basic requirement: print a counter (counting up or down) in a fixed
> frequency. Either a tool that does that or a library that I can use to
> code it.
>
> Bonus: execute a command with a fixed frequency and in contrast to
> watch(1) even when executing the command takes longer than the interval.
>
> So is there a utility that just implements a simple, precise counter?
>
> Is there a better version of watch(1) that is not fullscreen, allows
> faster than 0.1 second intervals and forks the application so that their
> runtime can exceed the interval time?



--
Karl E. Jorgensen
IT Operations


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20120502135713.GM18412@hawking">http://lists.debian.org/20120502135713.GM18412@hawking
 
Old 05-02-2012, 02:33 PM
Paul E Condon
 
Default unix tool as precise counter/timer for periodic print/exec

On 20120502_102011, Johannes Schauer wrote:
> Hi,
>
> I'm looking for a unix tool that does nothing else than increment and
> print an integer with a fixed frequency. As a bonus it should be able to
> execute a command with a fixed frequency. The special requirement: it
> should precise in the interval.
>
> Thus, the following will not work:
>
> #!/bin/sh
> while true; do
> print_counter
> sleep 1
> done
>
> because each loop iteration will take one second plus how long
> print_counter takes to execute.
>
> Even the following will not work:
>
> #!/bin/sh
> while true; do
> print_counter &
> sleep 1
> done
>
> Because even the time it takes to fork print_counter will accumulate
> over time.
>
> Another inconvenience of the above is, that not all sleep(1)
> implementations do accept floating point arguments to run the above
> faster than every second.
>
> What I need is something that on each step recalculates how long to
> sleep based on the current time so that the overall frequency remains
> stable.
>
> I do not need each print_counter to be executed exactly at a very
> precise moment but just that the overall frequency stays the same. After
> having it run 1000 times with a frequency of 1Hz I want 1000 seconds to
> have passed.
>
> Some libraries like glib provide functions like g_timout_add or
> g_timeout_add_seconds but that does not 'catch up' as I require above.
> Is there some library that does?
>
> There is the watch(1) tool which already partly does what I want using
> the --precise switch. But it runs in fullscreen and even when using
> --precise, it will not compensate for commands that take longer than the
> given interval length as it doesnt fork them. Also, watch(1) will not
> allow intervals smaller than 0.1 seconds.
>
> So basic requirement: print a counter (counting up or down) in a fixed
> frequency. Either a tool that does that or a library that I can use to
> code it.
>
> Bonus: execute a command with a fixed frequency and in contrast to
> watch(1) even when executing the command takes longer than the interval.
>
> So is there a utility that just implements a simple, precise counter?
>
> Is there a better version of watch(1) that is not fullscreen, allows
> faster than 0.1 second intervals and forks the application so that their
> runtime can exceed the interval time?
>
> cheers, josch

Don't use sleep, or any of the loop structures that you discuss above.
Use crontab (see man crontab). This is not the /etc/crontab that drives
some system services, but the one that provides a crontab for each user
Typing

crontab -e <return>

in an xterm, will get you started, but perhaps you should read the man
page first. Your crontab file will be created automagically in /var
where the system expects it to be.

I think you should be careful to not set the schedule to execute you
job too frequently. This system will launch a new job on schedule,
whether or not the previously launched job has completed. For some
jobs this can cause problems. If you want to do something that takes 1
or 2 sec., every 5 sec., crontab works without using locks on files,
or other safety mechanisms. Or look at the package liblockfile1.


--
Paul E Condon
pecondon@mesanetworks.net


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20120502143339.GB13647@big.lan.gnu">http://lists.debian.org/20120502143339.GB13647@big.lan.gnu
 
Old 05-02-2012, 02:44 PM
"Lars Noodén"
 
Default unix tool as precise counter/timer for periodic print/exec

On 5/2/12 5:33 PM, Paul E Condon wrote:
> I think you should be careful to not set the schedule to execute you
> job too frequently. This system will launch a new job on schedule,
> whether or not the previously launched job has completed. For some
> jobs this can cause problems. If you want to do something that takes 1
> or 2 sec., every 5 sec., crontab works without using locks on files,
> or other safety mechanisms. Or look at the package liblockfile1.

It could be wrapped in a script and the script could manage locking, say
through flock.

Regards
/Lars


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 4FA1483A.80002@gmail.com">http://lists.debian.org/4FA1483A.80002@gmail.com
 
Old 05-02-2012, 03:06 PM
Johannes Schauer
 
Default unix tool as precise counter/timer for periodic print/exec

Hi Paul,

> Use crontab (see man crontab).

But as far as I see, cron doesnt do what I want.

First of all, it will run as a daemon in the background so there is no
way (at least as far as I am aware) to make it output a counter/timer in
my terminal.

Second, as also mentioned in my initial email, I would also like a
resolution higher than once per second which is (afaik) also not what
cron is made for.

Thank you for your reply!

cheers, josch


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20120502150656.GA15709@hoothoot">http://lists.debian.org/20120502150656.GA15709@hoothoot
 
Old 05-02-2012, 04:18 PM
Johannes Schauer
 
Default 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


--
To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.org
Archive: 20120502161840.GB15709@hoothoot">http://lists.debian.org/20120502161840.GB15709@hoothoot
 

Thread Tools




All times are GMT. The time now is 01:25 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2007 - 2008, www.linux-archive.org