Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Perl Timer

by Anonymous Monk
on Dec 18, 2002 at 21:41 UTC ( [id://220953]=perlquestion: print w/replies, xml ) Need Help??

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

Hello,

Does anyone know if there is an easy way to run a script contunosuly everyday and have it sleep until 1:00 AM, where it then runs until completion, and then sleeps again until the following day at 1:00 AM

I have thought about using the sleep command but depending on when the script gets started, it needs to determine how long before 1:00 AM, which actually I can calculate when the script gets executed.
Thanks

Replies are listed 'Best First'.
Re: Perl Timer
by Nitrox (Chaplain) on Dec 18, 2002 at 21:48 UTC
    Which environment?

    Win32 look at 'AT'.

    Un*x look at 'Cron'.

    -Nitrox

      I wish it was Unix, so I could use cron jobs, but unfortunately its Win32
Re: Perl Timer
by djantzen (Priest) on Dec 18, 2002 at 21:48 UTC

    It sounds like the only reason to have it running continuously is to sleep, which means you're looking at the problem from the wrong angle. You need to rely on the OS to schedule execution at a particular date/time. What you're looking for is either cron under *nix, or the Task Scheduler under Windows NT/2000/XP.

      Well, not to through another wrench into the mix, but it may have to be run once every hour for a 24 hour period ... hmmm ... maybe I should look at the Task Scheduler in Win32.

      Thanks

        I agree with your comment and would definitely recommend you wrap your script into a batch file either via pl2bat or by handrolling a batch file to interact with your program. Then whack it into task scheduler.

        I've found this to be the best way on win32 (for me at least). On the win2k environments I work with I can also allow my programs to run as particular users. You could use the runas command to replicate this (assuming your windows platform supports it).

        ie:
        C:\>runas RUNAS USAGE: RUNAS [/profile] [/env] [/netonly] /user:<UserName> program /profile if the user's profile needs to be loaded /env to use current environment instead of user's. /netonly use if the credentials specified are for remote acc +ess only. /user <UserName> should be in form USER@DOMAIN or DOMAIN\ +USER program command line for EXE. See below for examples


        Hope that helps :)
Re: Perl Timer
by hawtin (Prior) on Dec 19, 2002 at 08:59 UTC

    Sometimes it is worth avoiding the local OS scheduling framework, for example when you want something portable.

    You can use localtime() and timelocal() (from Time::Local) to convert between absolute number of seconds and a 9 element array giving dates.

    # This is from memory and almost certainly contains bugs use Time::Local; $now = time; @now_array = localtime($now); # 1:00am is tomorrow if it is after that today if($now_array[2] >= 1) { $now_array[3]++; } # Set 1:00am in the structure $now_array[2] = 1; $now_array[1] = 0; # Convert to seconds $then = timelocal(@now_array); # Now wait sleep($then - $now);

    It is also a good idea not to sleep for the whole time but for an hour or so at a time (because clocks drift and long sleeps are sometimes inaccurate. Another thing that could be important is to arrange for your process to be restarted if it dies and to make sure there is only one of them running.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-20 05:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found