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


