pyro.699 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a script that is designed to monitor network activity for me. Every hour i would like it to send me an email but i donot know how i can set it up so that it will be emailed to me every hour... I would like to have a loop that is never ending, and some sort of wait command, that will run the script before its self and when the timer hits zero it will resume and end up back at the timer... if that sounds confusing then heres what id like it to look like...
endless_loop() { #script line 1 #script line 2 #script line 3 #script line 4 wait (3600); }
Thanks Cody

Replies are listed 'Best First'.
Re: Pause Loop
by shigetsu (Hermit) on Apr 27, 2007 at 23:47 UTC

    Have you considered using a cronjob instead?

    Then the loop would become superfluous and you could make it run the entire script without a stop.

Re: Pause Loop
by syphilis (Archbishop) on Apr 27, 2007 at 23:58 UTC
    Hi Cody,
    Try:
    while(1) { #script line 1 #script line 2 #script line 3 #script line 4 sleep (3600); }
    See "perldoc -f sleep".

    Cheers,
    Rob
      It seems that the sleep command will total all the commands there, i tried
      while(1) { print 1; sleep (1); }
      And nothing displayed. Thanks for the looping part though :)

        You must not have allowed it to run long enough to fill the output buffer. Prepend the line,

        $| = 1;
        before the loop to turn off buffering.

        After Compline,
        Zaxo

Re: Pause Loop
by BrowserUk (Patriarch) on Apr 28, 2007 at 05:51 UTC

    How accurate do you want the delay to be? If you embed a 1 hour delay in the loop along with the commands that are being run, your script will run every 1 hour + however long the commands take, so your schedule will slip continuously. A slightly different formulation of the loop will account for that without having to accurately time how long the commands take:

    my $endTime = time() + 3600; while( 1 ) { # Do stuff, # Do more stuff # And more # And more # And more # And more sleep 1 while time() < $endtime; $endtime = time() + 3600; }

    The inner sleep loop will consume negligible cpu but will ensure that you don't sleep longer than you need to, even if the active part of the script varies in how long it takes to run.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      while (1) { my $start_time = time(); ## do stuff here sleep 3600 - time() + $start_time; }
      TIMTOWTDI:
      while (1) { # Do stuff here; my $next = int((time + 3599)/3600) * 3600; sleep ($next - time); }
Re: Pause Loop
by thezip (Vicar) on Apr 27, 2007 at 23:53 UTC

    pyro.699, what is your OS? If you're on one of the Unices, why not just have the crond handle it? This would have the benefit of not having to poll constantly ...


    Where do you want *them* to go today?
      I would use cronjobs except the script will be moving from pc to pc and will not be stationary. Some are Windows XP others Are Ubuntu Linux, it would just be easier if it had a nevereding loop :) Thanks though

        Given the assumption that you'll need to poll, you could use something like MIME::Lite to send email in an OS agnostic manner...

        Update:

        This probably isn't such a good solution since MIME::Lite is not core Perl and you'd need to install it on each target machine.

        Nonetheless, you'll have difficulties getting the email part to work due to OS differences...

        What is your plan of attack for this?


        Where do you want *them* to go today?
Re: Pause Loop
by fmerges (Chaplain) on Apr 29, 2007 at 20:40 UTC

    Hi,

    you can do it using the time and sleep but you can also take a look at Schedule::Cron can be useful in the future once it extends or you want to do more fancier stuff.

    Regards,

    fmerges at irc.freenode.net