A cron expression is a string that defines a schedule for executing commands or tasks on Unix-based systems. It includes five fields separated by spaces, each representing a different time unit: minutes, hours, day of the month, month, and day of the week. An optional sixth field specifies the command to be executed. The basic format is as follows:
* * * * * * command_to_run
| | | | | |
| | | | | +--- Day of the week (0 - 7) (Sunday = 0 or 7)
| | | | +----- Month (1 - 12)
| | | +------- Day of the month (1 - 31)
| | +--------- Hour (0 - 23)
| +----------- Minute (0 - 59)
+------------- Second (0 - 59)
Field Descriptions
- Second: Defines the second of the minute (0-59) when the command will run.
- Minute: Defines the minute of the hour (0-59) when the command will run.
- Hour: Specifies the hour of the day (0-23).
- Day of the Month: Represents the specific day of the month (1-31).
- Month: Indicates the month (1-12).
- Day of the Week: Specifies the day of the week (0-7), where both 0 and 7 represent Sunday.
Special Characters
*: Any value in the specified field. For example,*in the minute field means "every minute.",: Allows multiple values. For example,1,15in the minute field runs the task at minutes 1 and 15.-: Defines a range of values. For instance,1-5in the hour field means "every hour from 1 to 5."/: Specifies increments. For example,*/10in the minute field indicates "every 10 minutes."
Example Cron Expressions
0 0 0 * * *: Runs daily at midnight.0 15 14 1 * *: Runs at 2:15 PM on the 1st day of every month.0 */5 * * * *: Executes every 5 minutes.0 0 22 * * 1-5: Runs at 10:00 PM Monday through Friday.0 0,10,20,30,40,50 * * * 1-5: Runs every 10 minutes Monday through Friday.
Cron expressions offer precise control over task scheduling, making them ideal for automating regular tasks.
