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

Hi Monks!

Is it possible to have a CRON job that will run a Perl script on every third week day of every first week of every month. Has anyone done anything like that? No luck asking this question around. I have this one that runs on every last Wednesday of every month , I assume this will be the path to my question:
0 7 * * 3 [ $(date +\%d) -le 03 ] && /script/script.pl

Thanks for the help!

Replies are listed 'Best First'.
Re: Cron to run a Perl script
by johngg (Canon) on Dec 09, 2015 at 14:27 UTC

    Rather than trying to get cron jumping through hoops, it would be much easier to invoke the script via cron every third weekday then inside the script test if it is the first week and bail if not. Get epoch seconds from time, calculate epoch seconds a week ago by subtracting 86400 * 7 seconds then use Time::Local::timelocal() to get the month number from each one; if the month isn't different you are not in the first week.

    I hope this is helpful.

    Update: A general note of caution with this approach, if your cron job is running an hour either side of midnight then beware of daylight savings changes in case they throw the calculations off. I've been bitten on the bum by that :-)

    Cheers,

    JohnGG

Re: Cron to run a Perl script
by Corion (Patriarch) on Dec 09, 2015 at 14:37 UTC

    Why not have the script exit unless it is run on a Wednesday, and then launching the script on the days 1 to 7 of the month?

Re: Cron to run a Perl script
by Maresia (Beadle) on Dec 09, 2015 at 19:26 UTC
    Had similar problem before, this will run every first Monday of every month:

    0 12 1-7 * * [ "$(date '+\%a')" = "Mon" ] && /script/script.pl

    I hope it helps!
Re: Cron to run a Perl script
by GotToBTru (Prior) on Dec 09, 2015 at 16:45 UTC

    If I understand you correctly, you'd want the job to run on Jan 5, Feb 3, Mar 3, Apr 5, May 4 those being the third weekday of the first week of the next few months. If not, please supply the list of actual dates you're looking for.

    Dum Spiro Spero
      I meant every third weekday or every Wednesday of the first week of every month. Thanks!

        So which is it .. every third weekday or every Wednesday? If you would supply the dates, as I did, your meaning would be obvious.

        Dum Spiro Spero
Re: Cron to run a Perl script
by Discipulus (Canon) on Dec 10, 2015 at 10:05 UTC
    I prefere the Corion's solution even if it has a drowback: when you lately inspect the cron schedules you not see the real runs. You can workaround this using a self explaining named argument to the final script or, if frequently used, a dedicated minimodule:
    0 7 * * 3 /script/script.pl --run_only_on_wednesdays #or 0 7 * * 3 perl -MExitUnlessWednsday script/script.pl


    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.