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.
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:
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
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.
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
Fill out our contact form and Garry or Sarah will get back to you.
When contacting us via this form, we'll only use your details to reply to your message. For more info please see our privacy policy