Cron expressions

Cron Expressions are a powerful way to define schedules for automated tasks. They are used to specify the precise timing for the execution of commands or scripts. Cron expressions consist of six fields that represent different units of time, allowing for highly customizable and flexible scheduling.

A typical cron expression follows this format:

* * * * * *
- - - - - -
| | | | | |
| | | | | +---- 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)

Description

  1. Second: The second of the minute when the command will run (0-59).

  2. Minute: The minute of the hour when the command will run (0-59).

  3. Hour: The hour of the day when the command will run (0-23).

  4. Day of the Month: The day of the month when the command will run (1-31).

  5. Month: The month when the command will run (1-12).

  6. Day of the Week: The day of the week when the command will run (0-7, where both 0 and 7 represent Sunday).

Day of the Month and Day of the Week cannot be used simultaneously. To prevent conflicts, use ? in one of these fields.

Examples

  • 0 0 0 * * ?: Runs at midnight every day.

  • 0 30 8 * * MON-FRI: Runs at 8:30:00 AM every weekday (Monday to Friday).

  • 0 0 3 1 * ?: Runs at 3 AM on the first day of every month.

  • 0 30 8 ? * 4: Runs every week on Wednesday at 8:30 AM

All times are based on UTC.

Special characters

  • * (asterisk): Represents all possible values for a field.

  • / (slash): Specifies increments. For example, 0/5 in the minute field means "every 5 minutes starting at minute 0."

  • , (comma): Separates multiple values. For example, 1,15 in the minute field means "at minute 1 and 15."

  • - (dash): Specifies a range. For example, 1-5 in the day of the week field means "Monday to Friday."

  • ? (question mark): Used in the day-of-the-month and day-of-the-week fields to indicate no specific value, preventing conflicts when both fields are specified.

  • L (last): Specifies the last day of the month or the last day of the week.

  • W (weekday): Specifies the nearest weekday to a given day of the month.

  • # (hash): Specifies the nth day of the month. For example, 2#1 means "the first Monday of the month."

Last updated