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

I'm writing an application, the first bit of which is basically a scan of a certain directory for new files which need parsing. If any files are found they'll be send to the second bit of the app for processing.

The directory scan needs to run like so:

I can do every small piece of this, except the major one which is to create a script that never exits, even if it sleep()s for hours at a time.

Using pseudocode, I've considered the following:

while ( currenttime is between 11PM and 6AM ) { scan directory every hour send found files to other process } # this will bail out if the time range is no good, # rather than sleeping, so that's no good while (1) { same process } # just seems stupid although that could just be me # wanting to overcomplicate things
Anyway, anyone have suggestion?

Replies are listed 'Best First'.
Re: sleep() but don't exit
by Corion (Patriarch) on Aug 12, 2005 at 17:09 UTC

    How about just running your script via the cron daemon, or using Schedule::Cron ?

      Or if running on a Windows machine, use the built in Scheduled Tasks (it is called that on WinXP at least) functionality found in the Control Panel.
Re: sleep() but don't exit
by davidrw (Prior) on Aug 12, 2005 at 17:29 UTC
    sounds like this really should just be kicked off as a cronjob. The cron entry will simply look like (run on the hour from 11PM to 6AM, inclusive):
    0 23-6 * * * /some/dir/yourscript
    Note that the big advantage of having cron deal with the scheduling is that you don't have to worry about your while(1){ ... } program getting hung or killed.
Re: sleep() but don't exit
by Transient (Hermit) on Aug 12, 2005 at 17:06 UTC
    Just wrap the whole thing in a while (1) loop:
    while (1) { while ( currenttime is between 11PM and 6AM ) { scan directory every hour send found files to other process } while( currenttime is not between 11PM and 6AM ) { sleep # whatever process you have here } }
    Or am I reading this incorrectly?
Re: sleep() but don't exit
by sh1tn (Priest) on Aug 12, 2005 at 19:14 UTC