run script one hour earlier the first weekday of the month

Homepage Clovertech Forums Read Only Archives Cloverleaf General run script one hour earlier the first weekday of the month

  • Creator
  • #50086
    Russ Ross

    I was asked if cron could schedule a script one hour earlier than normal on the first weekday of the month, ignoring holidays.

    Currently the script is scheduled via cron to kick off M-F at 6:30 PM even on holidays.

    I googled at first for a solution and then searched clovertech a bit.

    My google search did mentioned what I had figured as a work around for cron’s supposed inability to do this.

    One possible solution is to have cron schedule the event for an hour earlier at 5:30 PM Monday thru Friday for the first 3 days of each month and wrap the following psudo logic around the real script to decide if this is the first week day of the month:


    Russ Ross

Viewing 4 reply threads
  • Author
    • #64847
      Kevin Kinnell

      Heya, Rob.

      The best way I know of for handling “fuzzy” dates is with the GNU date

      command.  Here’s a little snippet of a Perl script I use for archiving:


      my $default_date;
       # local scope
       my $dc;
       open( $dc,’-|’,’date’, @{[ ‘-Idate’, -d => ‘last sunday’ ]} );
       $default_date = <$dc>;
       close $dc;
       chomp $default_date;

      Ignoring all the Perlish-ness, you can see that the date I’m using is “last



    • #64848
      James Cobane

      I’m probably over-simplifying what is desired, but couldn’t this be facilitated via 3 distinct cron entries:

      30 17 1 * 1-5 $HCIROOT/usercmds/myscript

      30 18 1 * 0,6 $HCIROOT/usercmds/myscript

      30 18 2-31 * * $HCIROOT/usercmds/myscript

      This first entry would run only on the first of the month for a weekday, the second entry runs on only on the first of the month for weekends,

      and the third runs the 2nd thru the 31st for all days.

      Jim Cobane

      Henry Ford Health

    • #64849
      Kevin Kinnell

      I forgot to mention gcal, which has even more options than date:


      $ gcal -q US_MS -n | grep ‘+ ‘
      New Year’s Day (US_MS)                   + Tue, Jan : 1st:2008 = -162 days
      Martin L. King’s Day (US_MS)             + Mon, Jan :21st:2008 = -142 days
      Presidents’ Day (US_MS)                  + Mon, Feb :18th:2008 = -114 days
      Remembrance/Memorial Day (US_MS)         + Mon, Apr :28th:2008 =  -44 days
      Remembrance/Memorial Day (US_MS)         + Mon, May :26th:2008 =  -16 days
      Independence Day (US_MS)                 + Fri, Jul : 4th:2008 =  +23 days
      Labour Day (US_MS)                       + Mon, Sep : 1st:2008 =  +82 days
      Veteran’s Day (US_MS)                    + Tue, Nov :11th:2008 = +153 days
      Thanksgiving Day (US_MS)                 + Thu, Nov :27th:2008 = +169 days
      Thanksgiving Day (US_MS)                 + Fri, Nov :28th:2008 = +170 days
      Christmas Day (US_MS)                    + Thu, Dec :25th:2008 = +197 days

      That’s all of the ‘official’ holidays this year (in Mississippi, anyway).  There

      may be some part of this program that can’t be configured as far as things

      like resources and output formats go, but I don’t know what it is.

    • #64850
      Kevin Kinnell

      Crimoney.  One last thing:

      When I need to do scheduled maybe-yes|maybe-no jobs with cron, I write

      a script that decides whether or not it should run, and run it unconditionally

      with cron.

      For your application, I would use the following sequence:

        set cron to run DoIt.script at 17:30 from the 1st to the 7th

        set cron to run DoIt.script at 18:30 Monday thru Friday.

      The script can signal itself not to run at 18:30 by touching a file on the first

      weekday of the month.

      What it would then do is (using an external program, maybe) get the required

      date info and:

        If it’s a holiday, exit

        If it’s not a weekday, exit

        If it finds a “signal” (say, a particular file) clear the signal and exit

        Otherwise, run.

      The tricky part is when they ask you to make it run one hour earlier on

      the first non-holiday weekday of the month, unless it’s February and the

      last two digits of the year are a prime number … but you can extend

      the logic in the script for that.  Just don’t try to get too fancy

      with cron (sorry, Jim) it’s almost always a bad idea.

      In other posts, I’ve mentioned that Timezones are a pain.  I should’ve

      mentioned that dates are also a pain.  Time, in general, is a pain.  In fact,

        “Time is an illusion.  Lunchtime, doubly so.”

                      –Ford Prefect

    • #64851
      James Cobane

      Yes, cron can be your friend, or your enemy.  I took “ignoring all holidays” as “don’t care if it’s a holiday or not”…kinda like some bosses I’ve known….

      Jim Cobane

      Henry Ford Health

Viewing 4 reply threads
  • The forum ‘General’ is closed to new topics and replies.

Forum Statistics

Registered Users
Topic Tags