Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

timer to run program function between 7 and 7 only

by Win (Novice)
on Nov 06, 2009 at 16:41 UTC ( [id://805521]=perlquestion: print w/replies, xml ) Need Help??

Win has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I would like to run a program function between 7pm and 7am only, repeatedly on multiple days. Would this requre use of a while loop. While the time is ok continue with everything within the loop - sort of thing????
  • Comment on timer to run program function between 7 and 7 only

Replies are listed 'Best First'.
Re: timer to run program function between 7 and 7 only
by Corion (Patriarch) on Nov 06, 2009 at 17:04 UTC

    Read up on cron. There are modules for Cron on CPAN.

Re: timer to run program function between 7 and 7 only
by MidLifeXis (Monsignor) on Nov 06, 2009 at 17:27 UTC

    So, to clarify, you want a long running program to start running at 7pm, and put itself to sleep at 7am.

    I would look at the sleep and localtime functions. Pay particular attention to the return value of the sleep function.

    --MidLifeXis

Re: timer to run program function between 7 and 7 only
by zentara (Archbishop) on Nov 06, 2009 at 17:53 UTC
    You could use an event loop, like this Glib example..... the trick is to return 0 or 1 to keep the timer going or to stop it. You could get very creative , depending on what code you need to run..... just fork and exec your code off, and let the timer do the checking for 7 to 7 in time.
    #!/usr/bin/perl use warnings; use strict; use Glib; my $main_loop = Glib::MainLoop->new; my $count = 1; my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 ); #1000 milliseconds = 1 second sub timer_callback{ $count++; # are we between 7 and 7?.... if so....(code left up to you) print "$count\n"; return 1; #return 1 to keep going, return 0 to stop timer } my $count1 = 1; my $timer1 = Glib::Timeout->add (100, \&timer1_callback, undef, 1 ); sub timer1_callback{ $count1++; print "\t$count1\n"; return 1; } ### filehandle watch #open (FH, "+> test.log") or warn "$!\n"; #Glib::IO->add_watch (fileno 'FH', ['in'], \&watch_callback, 'FH', 1 ) +; $main_loop->run;

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
Re: timer to run program function between 7 and 7 only
by JavaFan (Canon) on Nov 06, 2009 at 17:14 UTC
    What's a program function? What kind of program are we talking about - is it running all the time, started on demand, ... ? Should the "program function" abort if the clock passes 7AM and it's still running? Or perhaps it should wait till 7PM? Why aren't you just checking the current time?
      No it shouldn't abort because I want it to continue again when the time slot starts the next day.
        Could you be a bit more elaborate on what it is what you want?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://805521]
Approved by planetscape
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-20 02:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found