Cron day of month and day of week requirements

Posted on 2006-05-29
Last Modified: 2013-12-16
Hi experts,

I'm trying to schedule a report to automatically run three days before a meeting.  The meeting participants want a few days to review the report before they meet.  The meeting is on the third thursday of the month.  So, I want to schedule a cron job to run 3 days before the third thursday of the month.

Looking at a calendar I see I want to send this on Mondays.  But only on Mondays that fall between the 12th and the 18th inclusive.  The following does not work.

30 9 12-18 * 1 /path/to/script

The above runs at 9:30 AM every Monday.  It also runs at 9:30 AM on the 12,13,14,15,16,17,18 of the month.  There seems to be an "OR" relationship between the "Day of Month" and "Day of Week" fields.  If either field is true the job runs.  

Is there a way to change the logic so that the script will only run if both fields are true?   In other words, the default behavior for cron is an OR relationship between Day of Month and Day of Week.  Is there a slick way to change this to an AND relationship?  That way the script will only run on Mondays that fall between the 12th and 18th of the month inclusive.

My current workaround looks like this ... cron calls script on the 12th-18th
30 9 12-18 * * /path/to/script

Inside the script I check that it is Monday before executing anything.

if [ `date +%a`=Mon ]
  ... script goes here

This works, but I don't like having schedule information in both cron and in my script.  It would be nice to keep all scheduling information in cron.
Question by:testbenchdude
    LVL 24

    Accepted Solution

    Hi testbenchdude,

    From my limited knowledge, I don't think that there is a solution using cron only.  You still have to use some scripts to help you.  However, you can make the script more generic and use them over a whole lots of other scripts.

    For example, you can do this for cron:

    30 9 * * 1 /path/to/nthday 3 && /path/to/script

    and nthday will return, true if it is the 3rd Monday of the month, false otherwise.  The discussion and scripts for nthday (and lastday) can be found here:

    Author Comment


    Thanks for the link.  It turns out that the 3rd Monday does not work.  What I'm trying to do is run the script on the Monday before the third Thursday.  Sometimes it is the second Monday and sometimes it is the third Monday.  It works out to Mondays that fall between the 12th and the 18th inclusive.

    However, your link does point to example scripts that I can modify.

    This is what I changed it to:
    30 9 12-18 * * /path/to/dow 1 && /path/to/script

    Cron tries the script every morning from the 12th through the 18th.  But it only runs if dow is true.  So this only runs on Mondays that fall between the 12th and 18th inclusive.  This is a nice workaround to implement an AND relationship between DOM and DOW instead of the built-in OR relationship.
    LVL 24

    Expert Comment

    hi testbenchdude,

    ooh.. yes, you solution with the cron will work.  I overlooked that part.  Thanks for updating EE.

    -- slyong

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now