Complete Guide to Cron Expressions: Syntax, Examples & Best Practices

Cron expressions are one of the most commonly used time specification formats in the field of scheduled tasks, widely used in Linux systems, CI/CD pipelines, cloud service scheduling, task queues, and more. However, Cron's syntax, while concise, is error-prone, and many developers often encounter issues where schedules don't execute as expected. This article will systematically explain Cron expression syntax rules, the meaning of each field, common special characters, help you quickly master Cron through numerous practical examples, and share common pitfalls and best practices for writing accurate and reliable scheduled tasks.

I. What is a Cron Expression

A Cron expression is a string format used to specify scheduled task execution times. It originated from the cron daemon in Unix systems and has now become the de facto standard for scheduled tasks.

1.1 Origin of Cron

The word Cron comes from the Greek word "chronos" (time). It first appeared in Unix systems for automatically executing tasks at specified times. With the development of technology, Cron expressions have been widely adopted in various systems and platforms:

  • Linux/Unix system crontab
  • CI/CD tools: GitHub Actions, GitLab CI, Jenkins, etc.
  • Cloud services: AWS CloudWatch, Alibaba Cloud Scheduled Tasks, Tencent Cloud SCF, etc.
  • Backend frameworks: Spring Scheduler, Node.js node-cron, etc.

1.2 Basic Structure

A standard Cron expression consists of 5, 6, or 7 fields separated by spaces:

  • 5-field format (standard Unix cron): minute hour day month weekday
  • 6-field format (with seconds): second minute hour day month weekday
  • 7-field format (with seconds and year): second minute hour day month weekday year

πŸ’‘ Note:Different implementations may have subtle differences. Please refer to specific documentation when using.

II. Syntax Details

Let's take a detailed look at the value ranges and special characters for each field.

2.1 Field Descriptions

Taking the 6-field format (second minute hour day month weekday) as an example:

  • Second: 0-59
  • Minute: 0-59
  • Hour: 0-23
  • Day of month: 1-31
  • Month: 1-12 or JAN-DEC
  • Day of week: 0-7 or SUN-SAT (both 0 and 7 represent Sunday)
  • Year: optional, 1970-2099

2.2 Special Characters

Cron expressions support various special characters:

  • * (asterisk): represents all possible values
  • , (comma): enumerates multiple values, e.g., 1,3,5
  • - (hyphen): represents a range, e.g., 1-5
  • / (slash): represents step, e.g., 0/15 means every 15 units starting from 0
  • ? (question mark): only used for day and weekday fields, means no specific value
  • # (hash): indicates the nth day of week, e.g., 6#3 means the third Friday
  • L: means last, e.g., L means the last day of the month
  • W: means nearest weekday, e.g., 15W means the nearest weekday to the 15th

III. Common Examples Collection

Learning by example is the fastest way to master Cron. Here are some of the most commonly used Cron expression examples.

3.1 Every Minute / Every Hour

  • Every minute: 0 * * * * ?
  • Every 5 minutes: 0 0/5 * * * ?
  • Every 10 minutes: 0 0/10 * * * ?
  • Every hour: 0 0 * * * ?
  • Every 2 hours: 0 0 0/2 * * ?

3.2 Daily

  • Every day at midnight: 0 0 0 * * ?
  • Every day at 8 AM: 0 0 8 * * ?
  • Every day at 9:30 AM: 0 30 9 * * ?
  • Every day at noon: 0 0 12 * * ?
  • Every day at 6 PM: 0 0 18 * * ?

3.3 Weekly

  • Every Monday at 9 AM: 0 0 9 ? * MON
  • Every Friday at 5 PM: 0 0 17 ? * FRI
  • Monday to Friday at 9 AM: 0 0 9 ? * MON-FRI
  • Weekends at 10 AM: 0 0 10 ? * SAT,SUN

3.4 Monthly

  • 1st of every month at midnight: 0 0 0 1 * ?
  • 15th of every month at 9 AM: 0 0 9 15 * ?
  • Last day of every month at 23:59: 59 59 23 L * ?

3.5 Specific Times

  • Every hour during work hours: 0 0 9-18 ? * MON-FRI
  • First day of each quarter: 0 0 0 1 1,4,7,10 ?
  • Every half hour: 0 0/30 * * * ?
  • 10 AM and 4 PM: 0 0 10,16 * * ?

IV. Advanced Usage

Mastering these advanced usages can handle more complex scheduling needs.

4.1 Workday Calculation

Using the W character you can specify the nearest weekday:

  • Nearest weekday to the 15th: 0 0 9 15W * ?
  • Last workday of the month: 0 0 9 LW * ?

4.2 Nth Day of Week

Using the # character you can specify the nth occurrence of a weekday:

  • Third Friday at 10 AM: 0 0 10 ? * 6#3
  • Second Monday at 9 AM: 0 0 9 ? * 2#2
  • Last Sunday: 0 0 0 ? * 7L

V. Common Pitfalls & Notes

Cron expressions may look simple, but there are many easy pitfalls.

5.1 Day vs Weekday Conflict

The most common issue is the conflict caused by specifying both day and weekday:

  • In most implementations, day and weekday have an "OR" relationship, not "AND"
  • 0 0 0 15 * 1 means the 15th of each month OR every Monday, not "the 15th AND a Monday"
  • If you only want to specify one, use ? for the other

5.2 Timezone Issues

Cron expression execution time depends on the system timezone:

  • Different servers may have different timezone settings
  • Cloud service Cron may use UTC timezone
  • DST transitions may cause tasks to execute once more or once less
  • It's recommended to explicitly specify the timezone in configuration

5.3 Edge Cases

Some edge cases that are easy to get wrong:

  • February 30th, 31st: will never trigger
  • February 29th: only triggers in leap years
  • Weekday values: both 0 and 7 represent Sunday
  • Start of month: midnight on the 1st is easily overlooked

VI. Best Practices

Follow these best practices to make your Cron expressions more reliable.

6.1 Readability First

Prefer more readable syntax:

  • Use 0 9 * * ? instead of 0 9-9 * * ?
  • Use MON-FRI instead of 1-5
  • Add comments explaining the expression's meaning
  • For complex expressions, use a generator to assist

6.2 Thorough Testing

Always test before using in production:

  • Use online Cron tools to verify expression meaning
  • Run in a test environment first to verify execution times
  • Record the last few and next few execution times
  • Set task timeout and failure retry mechanisms

6.3 Monitoring & Alerting

Production scheduled tasks must have monitoring:

  • Log task execution
  • Set up failure alert notifications
  • Monitor if task execution time is abnormal
  • Ensure only one instance is running (prevent duplicate execution)

VII. Using TudoSi Cron Expression Generator

TudoSi Tools provides convenient Cron expression generation and parsing features:

⏰

Cron Expression Generator

Visual generate / parse / execution time preview

TudoSi Tools' Cron expression generator provides a visual configuration interface to easily generate various complex Cron expressions. Supports both second-level and minute-level modes, with real-time display of the next 10 execution times to help you verify expression correctness. Provides common expression templates for one-click use, plus detailed syntax explanations and examples. An essential tool for DevOps and developers.

Visual generation Execution time preview Common templates Syntax guide
Use Now β†’

VIII. Summary

Cron expressions are one of the fundamental skills every developer should master. Although the syntax is concise, writing accurate and reliable Cron expressions is not easy. Through this article, I believe you have mastered Cron's basic syntax, common special characters, example patterns for various scenarios, and common pitfalls to watch out for.

In practical use, remember the following points: First, understand the "OR" relationship between day and weekday β€” this is the most common mistake. Second, pay attention to timezone issues, especially in cloud services and distributed systems. Finally, always use tools to verify your expressions and thoroughly test in a staging environment before deploying to production.

TudoSi Tools' Cron expression generator can help you quickly generate and verify Cron expressions. The visual configuration interface means you never have to memorize syntax rules again, making it an indispensable tool for DevOps and developers.