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

Good Morning Monks!

I'm looking for some basic perl code to control timed events. I do not need a module per say or a timer just something basic like this: Monday: search files and move log data here and Friday destroy daily log data and move to long term storage folder. The move/destroy/make commands I've done numerous times using Perl Code....I guess what I am looking for is some type of "watcher" code? The perl script will be setup via crontab to run everyday at a given time. Thanks in advance!

Replies are listed 'Best First'.
Re: Timed Events
by choroba (Cardinal) on Dec 10, 2015 at 16:31 UTC
    The fifth field in the crontab is the day of week:
    * * * * * command to be executed - - - - - | | | | | | | | | +----- day of week (0 - 6) (Sunday=0) | | | +------- month (1 - 12) | | +--------- day of month (1 - 31) | +----------- hour (0 - 23) +------------- min (0 - 59)

    No need to check the day again in the code.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

      To follow up on this a bit -- if you include the date in your code as well as in the execution control, you now have to update your code if it is necessary to run this off schedule, if the schedule changes, if an execution failed, etc.

      Split your concerns. Have the app do what it does well, and have cron control when it runs.

      --MidLifeXis

        All three answers were great. It is an older server which needs replaced however management says just to let it run and when it dies tend to it then. So using the day of the week instead of relying on the crontab for the day will be more reliable as the machine likes to hiccup especially on weekends and it sends the internal date and time off....and especially when the power goes out for a longer period of time than the UPS allows for. Thanks again!

Re: Timed Events
by toolic (Bishop) on Dec 10, 2015 at 16:27 UTC
    Time::Piece (a Core module) can tell you what day of the week it is when your cronjob runs your script every day:
    use warnings; use strict; use Time::Piece qw(localtime); my $t = localtime; my $today = $t->day(); if ($today eq 'Mon') { # search and move } elsif ($today eq 'Fri') { # destroy }