Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Cron day of month and day of week requirements

Posted on 2006-05-29
Medium Priority
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
  • 2
LVL 24

Accepted Solution

slyong earned 750 total points
ID: 16787976
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

ID: 16790333

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

ID: 16790665
hi testbenchdude,

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

-- slyong

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How many times have you wanted to quickly do the same thing to a list but found yourself typing it again and again? I first figured out a small time saver with the up arrow to recall the last command but that can only get you so far if you have a bi…
Fine Tune your automatic Updates for Ubuntu / Debian
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

569 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