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|
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
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
Fill out our contact form and Garry or Sarah will get back to you.
01323 505 980
9am-5pm Monday to FridaySwitchplane Ltd