1. Overview
Simply put, cron is a basic utility available on Unix-based systems. It enables users to schedule tasks to run periodically at a specified date/time. And it’s naturally a great tool for automating lots of process runs, which otherwise would require human intervention.
Cron runs as a daemon process. This means it only needs to be started once and will keep running in the background. This process makes use of crontab to read the entries of the schedules, and it kicks off the tasks at the specified times.
Over time, the cron expression format became widely adopted, and many other programs and libraries made use of it.
Learn how to schedule jobs with the Quartz API.
How to use the @Scheduled annotation in Spring, to run tasks after a fixed delay, at a fixed rate or according to a cron expression.
2. Cron Expression
Let’s understand the cron expression. It consists of five fields:
<minute> <hour> <day-of-month> <month> <day-of-week> <command>
Next, let’s begin with some commonly used examples of cron expressions and then look at some special characters in cron expressions.
2.1. Cron Expression Examples
To get a clear overview and easier comparison, let’s put examples in a table:
| Cron Expression |
Description (24-hour formats) |
| 0 12 * * ? |
Run at noon (12:00) every day |
| 0/15 0 * * ? |
Run every 15 minutes every day |
| 1/2 0 * * ? |
Run every odd minute |
| 0/5 13,18 * * ? |
Run every five minutes, starting at 13:00 and ending at 13:55, and then starting at 18:00 and ending at 18:55, every day |
| 0-5 13 * * ? |
Run every minute starting at 13:00. and ending at 13:05 every day |
| 15,45 13 ? 6 Tue |
Run at 13:15 and 13:45 every Tuesday in June |
| 30 9 ? * MON-FRI |
Run at 09:30 every Monday, Tuesday, Wednesday, Thursday and Friday |
| 30 9 15 * ? |
Run at 09:30 on the 15th day of every month |
| 0 18 L * ? |
Run at 18:00 on the last day of every month |
| 0 18 L-3 * ? |
Run at 18:00 on the third to last day of every month |
| 30 10 ? * 5L |
Run at 10:30 on the last Thursday of every month |
| 0 10 ? * 2#3 |
Run at 10:00 on the third Monday of every month |
| 0 0 10/5 * ? |
Run at 00:00 on every 5th day, starting from the 10th until the end of the month |
In the above cron expression examples, we used special characters, such as ‘*‘, ‘?‘, ‘–‘, ‘L‘, and so on.
Next, let’s take a closer look at those characters.
2.2. Special Characters in Cron Expressions
Let’s summarize the special characters used in cron expressions:
| Symbol |
Description |
Examples |
| * |
For every time unit |
‘*’ in the <minute> field means “for every minute” |
| ? |
Used in the <day-of-month> and <day-of-week> fields to denote the arbitrary value and thus neglect the field value. |
‘?’ in the <day-of-week> field means “any week day” |
| –Â |
Determines the value range |
10-15 in the <hour> field means “between 10th and 15th hours” |
| , |
Specifies multiple values |
“MON, WED, FRI” in <day-of-week> field means on the days “Monday, Wednesday and Friday.” |
| / |
Defines the incremental values |
“5/15” in the <minute> field means “5, 20, 35, and 50 minutes of an hour.” |
| L |
Used in the <day-of-month> and <day-of-week> fields, indicates the last day of the month or the last occurrence of the specified weekday in a month. Also, ‘L’ supports an offset value. |
- ‘L’ in <day-of-month> – the last day of the month, regardless of the month length, such as January 31, February 28 or 29
- “L-3” in <day-of-month> – The third to last day of the calendar month
- “5L” in <day-of-week> – The last Friday of the month
|
| W |
Used in the <day-of-month> field to specify the nearest weekday (Monday through Friday) to a given day. W is most useful for jobs that need to avoid running on weekends. |
“10W” in <day-of-month> – weekday near to 10th of that month:
- If the “10th” is a Saturday – The job runs on 9th (Friday)
- If the “10th” is a Sunday – The job runs on 11th (Monday)
- If the “10th” is a weekday – The job runs on 10th
|
| # |
Specifies the “N-th” occurrence of a weekday of the month, format: DAY_OF_THE_WEEK#OCCURRENCE_NUMBER |
- “5#3” in <day-of-week> – The third Friday of the month
- “1#2” in <day-of-week> – The second Monday of the month
|
As we can see, these special characters allow us to create cron expressions flexibly.
3. Cron Special Strings
In addition to the fields specified in the cron expression, there’s also support for some special, predefined values that we can use instead of the fields:
- @reboot – run once at system startup
- @yearly or @annualy – run once a year
- @monthly – run once a month
- @weekly – run once a week
- @daily or @midnight – run once a day
- @hourly – run hourly
4. Working With Crontab
A cron schedule is a simple text file located under /var/spool/cron/crontabs on Linux systems. We cannot edit a crontab file directly, so we need to access it using the crontab -e command:
crontab -e
This opens the file for editing.
Each line in crontab is an entry with an expression and a command to run:
* * * * * /usr/local/ispconfig/server/server.sh
This entry runs the mentioned script every single minute.
5. Conclusion
In this quick article, we’ve explored cron jobs and crontab.
We’ve also seen several examples we can use in our daily work or simply infer other expressions from.