We have monitoring scripts that need to be run every second. The scripts execution takes longer than a second, but that is not a problem.
So we have need a function that would run the script every second. The interval must be as precise as possible. I know that the preciseness depends on CPU, RAM, CMOS etc. I don’t care about these.
Currently, we use a script, let’s call it negative_timer. It works quite precisely, but the Time::HiRes::sleep from time to time complains that negative time not invented yet. A second using this script is in average about 0.000000676723442 less than a real second.
The problem is that the negative_timer substracts a second from a $max value. Sometimes the NTP kicks in and it makes the trouble. We don’t case if a second would be longer/shorter when the NTP updates the time.
Actually, we don’t need any $max value, as we run the monitoring script indefinitely (i.e. until seldom reboot or maintaince).
Could anyone help me out in this?
Thank you in advance.
The negative_timer script:
use strict; use warnings; use Time::HiRes qw/time sleep/; sub negative_timer { my $max = $_[0]; my @time_test; my $start = time(); for (my $i = 0; $i < $max; $i++) { $start += 1; sleep $start - time(); $time_test[$i] = time(); } return @time_test; }
In reply to The most precise second (timer) by tukusejssirs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |