[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: 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


Reply to: