Posted by Joel on 20th February 2009
How to run a Cron job on the first weekday of tÂhe month
The first weekday of the month can fall on the 1st, 2nd or 3rd day of the month:
| Month Begins | First Weekday | Date of First Weekday |
|---|---|---|
| Saturday | Monday | 3rd |
| Sunday | Monday | 2nd |
| Monday | Monday | 1st |
| Tuesday | Tuesday | 1st |
| Wednesday | Wednesday | 1st |
| Thursday | Thursday | 1st |
| Friday | Friday | 1st |
You can see from the table that:
- If today is a Monday, then the job should be run if today is also the 1st, 2nd, or 3rd of the month
- If today is a Tuesday, Wednesday, Thursday, or Friday, then the job should be run if today is also the 1st of the month
The crontab records
This will run your job at 10am on the first weekday of the month:
#-- First weekday of the month
# If it's Monday, and it's the 1st, 2nd or 3rd day of the month
00 10 1-3 * * [ "$(date '+\%a')" == "Mon" ] && /path/to/your/job.sh
# If it's Tues - Friday, and the 1st of the month
00 10 1 * * [ "$(date '+\%a')" == "Tue" ] && /path/to/your/job.sh
00 10 1 * * [ "$(date '+\%a')" == "Wed" ] && /path/to/your/job.sh
00 10 1 * * [ "$(date '+\%a')" == "Thu" ] && /path/to/your/job.sh
00 10 1 * * [ "$(date '+\%a')" == "Fri" ] && /path/to/your/job.sh
#-- End first weekday of the month
Note 1: Parameters are OR
The day of month and day of week parameters are combined using OR rather than AND. If parameters were combined using AND, it would be possible to use the following crontab entries to run a command on the first weekday of the month:
00 10 1-3 * 1 /path/to/your/job.sh
00 10 1 * 2-5 /path/to/your/job.sh
The above doesn't work! Instead, the job is run on every weekday, twice on the 1st of the month, and maybe twice on the 2nd and 3rd of the month!
Note 2: Percent signs escaped
Percent signs in the crontab entry command are converted to newlines. They must be escaped using a backslash otherwise you will see an error message like this:
/bin/sh: -c: line 1: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 2: syntax error: unexpected end of file
Joel says On 19th April 2011 at 11:57
I don't think so, but I'm not an expert on the command line.
You can find out by trying it in your shell:
date '+%a'
If you get "Tue" back then you don't need the path.
Dinesh says On 19th April 2011 at 11:36
Hi, one more doubt do we need to add any path in cron for execution of "$(date '+\%a')"
== "Tue" ]
Dinesh says On 19th April 2011 at 11:03
Hi, Joel
Thanks, Yes i am doing this only on April 19 when. it falls on tuesday. not on every tuesdays
Joel says On 19th April 2011 at 10:39
By the way, are you trying to only ever do this on a Tuesday that is also the 19th April?
Joel says On 19th April 2011 at 10:34
Hi Dinesh, I think you are missing the && before your mkdir. Try the following:
42 14 19 04 * [ "$(date '+\%a')" == "Tue" ] && mkdir /root/test
Dinesh says On 19th April 2011 at 10:17
Thanks,
Its very useful, but i run this job in cron
42 14 19 04 * [ "$(date '+\%a')" == "Tue" ] mkdir /root/test
i.e a directory test has be created at 14:42 on Tue of Apr 19th but its not working for me.
Add A Comment
Recent Blogs
Gradient Meshes in Adobe Illustrator
There's more to Adobe Illustrator than basic shapes and blocks of colour, and there's more to gradients than linear or radial effects. Read More »
Web Design Trends 2011
Web designers are shying away from creating gimmicky tricks, but rather clean, accessible, bug-free coding - that works. Read More »
Let's Do Business - Eastbourne 2011
Find out what Switchplane did at Let's Do Business Eastbourne 2011. Read More »
Integrating with Kashflow using PHP
Find out how to use PHP's SoapClient class to integrate with Kashflow. Includes sample code and best practice tips. Read More »
Improving Search with Levenshtein Distance
Find out how we've made Honey Barrett's document management system search facility handle spelling mistakes. Read More »
The New Switchplane Website
Our new website is ready - read about what we've updated and why, and get ready for Project Awesome! Read More »
Website Accessibility
Have you ever considered the importance of accessibility on your website? Read More »
How Amazing Databases Are
A blog post about the usefulness of databases, using a person management system as an example. Read More »
Reasons to use Vector Graphics over Rasters
When should you use vectors or rasters, and does it even matter? Read More »
Some thoughts on writing for your business website...
Read some tips on preparing copy for your website. Read More »
Let's Do Business - Hastings 2010
Switchplane attended LDB Hastings in 2010 - read about our day. Read More »
Lets Do Business Eastbourne 2010
Switchplane's experience at Let's Do Business 2010 in Eastbourne. Read More »
Web Design Trends 2010 Part 1
A guide to web design trends 2010, including hand-drawn and painted layouts, typefaces, modern vectors and large headers and footers. Read More »
Common Printing Problems and how to avoid them
Even if it looks good on your screen, it isn't guaranteed to come out like that! I've selected a few of the most common print problems to watch out for. Read More »
Web design trends 2009 Part 2: Old and Torn Paper
Make your website a real piece of personal artwork by collecting and scanning-in torn and crumpled paper and using it for the design of your website (or just take a visit to istock photo!). Read More »
Web design trends 2009 Part 1: Badges
Subtle elements and new layout ideas for 2009. My guide on how to make your website look fashionable and unique, packed full of information and further resources. Read More »
The Joys of JQuery
JQuery is a light-weight, cross-browser and feature packed JavaScript library. To explain why JQuery is so good, first you need to know why not using it is so bad! Read More »
Lets Do Business Eastbourne 2009
Last Thursday (25th June) Switchplane attended their first Lets Do Business event at the Winter Gardens in Eastbourne. Read More »
Top 10 Sales Tips
Times are tough. Sales are down. Morale is low. Sound familiar? Well it needn't be. Below are a few tips which are sure to help you re evaluate and succeed in pushing your sales margins. Read More »
Create printable artwork in Indesign and Photoshop
A few useful steps to help you successfully prepare your artwork for print, using Adobe Photoshop and Adobe Indesign. Learn about colour modes, resolution, size and other helpful tips! Read More »
Search engine ranking more important than ever
Search engine optimisation is now an essential part of a successful website. And because of the way search engines now work, the focus of SEO is now about producing quality content and getting your name out there so you get incoming links. Read More »
Four Months at Switchplane
I've been at Switchplane for four months now - to celebrate the launch of our new website I'm sharing some thoughts on what it's like working here. Read More »

Dinesh says On 19th April 2011 at 12:14