How to run a cron job on the first weekday of the month

Often it's useful to generate automatic reports or perform some other task at the start of the month. Find out how to schedule a cron job to run on the first weekday of each month.

Cron runs a tight ship.

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:

# Monday, and the 1st, 2nd or 3rd day of the month
00 10 1-3 * * [ "$(date '+\%a')" == "Mon" ] && /path/to/your/job.sh

# 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

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
The Switchplane Team

Hey! We are Switchplane and we help businesses save time and money by building them custom software. This can be anything from a job management portal to a complex ecommerce solution. Want to find out more?

About Switchplane

Contact Us

Can we help with your project?

Get in touch

Join our Newsletter

Sign up to our monthly newsletter to keep up to date on the latest Switchplane and sector news.

You can unsubscribe at any time. View our Privacy Policy