Question

I am writing a script to capture disk usage on a system (yes, I know there is software that can do this). For database reporting purposes, I want the interval between data points to be as equal as possible. For example, if I am polling disk usage every 10 minutes, I want every data point to be YYYY-MM-DD HH:[0-5]0:00. If I'm am polling every 5 minutes, it would be YYYY-MM-DD HH:[0-5][05]:00.

If I have a ksh script (or even a Perl script) to capture the disk usage, how can I let the script come active and wait for the next "Poll time" before taking a snapshot, and then sleep for the correct number of seconds until the next "Poll time". If I am polling every 5 minutes, and it is 11:42:00, then I want to sleep for 180 seconds so it will take a snapshot at 11:45:00 - and then sleep for 5 minutes so it will take another snapshot at 11:50:00.

I wrote a way that works if my poll time is every 10 minutes, but if I change the poll time to a different number, it doesn't work. I would like it to be flexible on the poll time.

I prefer to do this in shell script, but if it is way too much code, Perl would be fine too.

Any ideas on how to accomplish this?

Thanks in advance!

Brian

EDIT: Wow - I left out a pretty important part - that cron is disabled, so I will not be able to use cron for this task. I am very sorry to all the people who gave that as an answer, because yes, that is the perfect way to do what I wanted, if I could use cron.

I will be using our scheduler to kick off my script right before midnight every day, and I want the script to handle running at the exact "poll times", sleeping in between, and exiting at midnight.

Again, I'm very sorry for not clarifying on crontabs.

Was it helpful?

Solution

If I were doing this, I would use the system scheduler (cron or something else) to schedule my program to run every 180 seconds.

EDIT: I might have misunderstood your request. Are you looking more for something along the following lines? (I suspect there is a bug or two here):

ANOTHER EDIT: Remove dependency on Time::Local (but now I suspect more bugs ;-)

#!/usr/bin/perl

use strict;
use warnings;

use POSIX qw( strftime );

my $mins = 5;

while ( 1 ) {
    my ($this_sec, $this_min) = (localtime)[0 .. 1];

    my $next_min = $mins * ( 1 + int( $this_min / $mins ) );
    my $to_sleep = 60 * int( $next_min - $this_min - 1 )
                 + 60 - $this_sec;

    warn strftime('%Y:%m:%d %H:%M:%S - ', localtime),
        "Sleeping '$to_sleep' seconds\n";

    sleep $to_sleep;
}

__END__

OTHER TIPS

cron will do the job.

http://en.wikipedia.org/wiki/Cron

Just configure it to run your ksh script at the times you need and you are done

You might want to consider using cron. This is exactly what it was made for.

Have it sleep for a very short time, <=1 sec, and check each time whether poll time has arrived. Incremental processor use will be negligible.

Edit: cron is fine if you know what interval you will use and don't intend to change frequently. But if you change intervals often, consider a continuously running script w/ short sleep time.

Depending on how fine grained your time resolution needs to be, there may be a need to write your script daemon style. Start it once, while(1) and do the logic inside the program (you can check every second until it's time to run again).

Perl's Time::HiRes allows very fine granularity if you need it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top