0 9 * * 1-5 runs at 09:00 on Monday, Tuesday, Wednesday, Thursday and Friday. The cron parser reads it as “At 09:00, on Monday through Friday” and the next-runs list skips Saturday and Sunday.
0 9 * * 1-5 /usr/local/bin/morning-report.sh >> /var/log/report.log 2>&1
The day-of-week field
| Number | Day | Name |
|---|---|---|
| 0 | Sunday | SUN |
| 1 | Monday | MON |
| 2 | Tuesday | TUE |
| 3 | Wednesday | WED |
| 4 | Thursday | THU |
| 5 | Friday | FRI |
| 6 | Saturday | SAT |
| 7 | Sunday again | SUN |
A range uses a dash (1-5), a list uses commas (1,3,5 for Monday, Wednesday and Friday), and names are not case sensitive in most versions. A range cannot wrap around the week, so “Friday to Monday” is written as the list 5,6,0,1. Stay away from the number 7 if the file must run on many systems; 0 is understood everywhere.
Common weekday schedules
0 9 * * 1-5 # 09:00 on weekdays
*/30 9-17 * * 1-5 # every 30 minutes between 09:00 and 17:30
0 9 * * 1,3,5 # 09:00 on Monday, Wednesday and Friday
0 10 * * 6,0 # 10:00 on the weekend
For two times on weekdays you need two lines (0 9 * * 1-5 and 30 14 * * 1-5), because the minute and the hour fields combine as a product.
Time zone: whose 9 AM?
The job runs at 9 AM on the server’s clock. On a UTC server the line above fires at 16:00 in Hanoi (UTC+7). To run at 9 AM Hanoi time on such a server, write the hour in UTC: 0 2 * * 1-5 is 09:00 in UTC+7. Near midnight the weekday shifts as well: 06:00 Monday in Vietnam is 23:00 UTC on Sunday, so the day field changes with the hour. Check the next runs in the parser after any conversion.
Holidays and exceptions
Cron has no calendar of holidays. The simplest approach is to run on every weekday and have the script exit quietly on a holiday:
#!/bin/sh
# skip dates listed in /etc/holidays, one YYYY-MM-DD per line
grep -qx "$(date +%F)" /etc/holidays && exit 0
exec /usr/local/bin/morning-report.sh
If you set a % in a crontab line, remember that cron treats it as a newline: write \% (for example in date +\%F). Moving the command to a script avoids the problem.
Related
The same idea for one day only is on every Monday at 9. Be careful when you combine a day of the month with a weekday, because cron joins them with OR, as explained there. Build and test an expression in the cron builder; for time zone details read cron and time zones.
Frequently asked questions
How do I run a cron job only on weekdays?
Put 1-5 in the day-of-week field: 0 9 * * 1-5 runs at 09:00 from Monday to Friday. Names work in many implementations: 0 9 * * MON-FRI.
Is Sunday 0 or 7?
Both mean Sunday in most cron versions, so Monday is 1 and Saturday is 6. Use 0 in portable files, because some implementations only accept 0 to 6.
Can cron skip public holidays?
No. It knows only the calendar fields. Run the job on weekdays and let the script decide: it can check a holiday list, or a calendar service, and exit when the day is not a working day.
How do I run at 9:00 and 14:30 on weekdays?
Use two lines: 0 9 * * 1-5 and 30 14 * * 1-5. A single line cannot combine two different minute and hour pairs, because the fields multiply (hours 9 and 14 with minutes 0 and 30 would give four runs).
More in this reference
- Cron daily at midnight (0 0 * * *): @daily, time zones and a better hour
- Cron every 5 minutes (*/5 * * * *): syntax, the step operator and its limits
- Cron every 6 hours (0 */6 * * *): steps in the hour field
- Cron every 15 minutes (*/15 * * * *): quarter hours and staggered starts
- Cron every hour (0 * * * *): @hourly, and the mistake of * 0 * * *
- Cron every minute (* * * * *): how it works and the traps
- Cron every Monday at 9 AM (0 9 * * 1) and the day-of-month OR trap
- Cron on the first day of the month (0 0 1 * *): @monthly and its limits
- Cron on the last day of the month: why there is no L, and three working solutions