Cron expressions are a powerful way to schedule tasks to run at specific times or intervals. This tutorial will cover the basics of Cron expressions, explain their components, and provide advanced examples and use cases to help you utilize them effectively.
What is a Cron Expression?
A cron expression is a string consisting of 6 subexpressions (fields) that describe individual details of the schedule.
How Do Cron Expressions Work?
Cron expressions are composed of 6 fields, each representing a unit of time. These fields are separated by spaces and define the precise schedule for when the task should run. The fields are:
Minutes (0-59)
Hours (0-23)
Day of Month (1-31)
Month (1-12 or JAN-DEC)
Day of Week (0-7 or SUN-SAT, where both 0 and 7 represent Sunday)
Year (optional, 1970-2199)
Each field can contain specific values, wildcards, and ranges to tailor the schedule precisely.
Cron Expressions Allowed Fields and Values
Field | Values | Wildcards |
Minutes | 0-59 | , - * / |
Hours | 0-23 | , - * / |
Day-of-month | 1-31 | , - * ? / L W |
Month | 1-12 or JAN-DEC | , - * / |
Day-of-week | 1-7 or SUN-SAT | , - * ? L # |
Year | 1970-2199 | , - * / |
Components of a Cron Expression
Each field in a Cron expression can use various wildcards to create flexible schedules:
Comma (,): Specifies additional values. Example:
1,2,3
in the minute field means the task runs at the 1st, 2nd, and 3rd minute.Dash (
-
): Specifies a range of values. Example:1-5
in the hour field means the task runs every hour from 1 to 5.Asterisk (
*
): Represents all possible values. Example:*
in the day-of-month field means the task runs every day of the month.Slash (
/
): Specifies increments. Example:*/5
in the minutes field means the task runs every 5 minutes.Question mark (
?
): Used in the day-of-month and day-of-week fields to indicate no specific value. Example:?
in the day-of-week field means any day of the week.L: Stands for "last". Example:
L
in the day-of-month field means the task runs on the last day of the month.W: Specifies the nearest weekday. Example:
15W
means the nearest weekday to the 15th of the month.#: Used in the Day-of-week field. It specifies a certain instance of the specified day of the week within a month. For example,
3#2
would be the second Tuesday of the month: the 3 refers to Tuesday because it is the third day of each week, and the 2 refers to the second day of that type within the month.
Example
Here are some examples of Cron expressions to help you better understand it:
0 0 * * *
: Runs every day at 00:00 AM (midnight).0 12 * * MON-FRI
: Runs every weekday at 12 PM (noon).0 18 1 * *
: Runs at 6 PM on the first day of every month.*/15 * * * *
: Runs every 15 minutes.0 8-17/2 * * *
: Runs every 2 hours between 8 AM and 5 PM.0 0 1 JAN *
: Runs once a year at midnight on January 1st.0 0 15W * *
: Runs on the nearest weekday to the 15th of every month.0 10,44 14 ? 3 WED
: Runs at 2:10 PM and 2:44 PM every Wednesday in March.
Conclusion
Cron expressions are a versatile and essential tool for automating repetitive tasks. By understanding the syntax and components, you can create schedules that meet a wide range of needs. Experiment with different expressions to see how they can simplify and enhance your workflows.