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

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


Reply to: