Cron Expressions Explained, with Real Examples
June 11, 2026 · MyAITools Team
A practical guide diving into cron expressions, with specific examples to aid in scheduling tasks effectively.
Introduction
Cron expressions are used to manage time-based job scheduling in Unix-like operating systems. Understanding how to craft these expressions is crucial for automating tasks efficiently. In this guide, we will delve into the syntax of cron expressions, illustrate their components, and provide practical examples to help you leverage them effectively.
Cron Expression Syntax
A typical cron expression consists of five fields that represent the time and frequency for task execution:
* * * * * command_to_execute
│ │ │ │ │
│ │ │ │ └─ Day of the week (0 - 7) (Sunday is both 0 and 7)
│ │ │ └─── Month (1 - 12)
│ │ └───── Day of the month (1 - 31)
│ └─────── Hour (0 - 23)
└───────── Minute (0 - 59)
Each field can contain:
- A single number (e.g.,
5for the 5th minute) - A wildcard
*for "every" - Ranges (e.g.,
1-5for Monday through Friday) - Lists (e.g.,
1,3,5for Monday, Wednesday, and Friday) - Step values (e.g.,
*/15for every 15 minutes)
Detailed Breakdown of Each Field
1. Minutes (0-59)
The first field specifies the minute when the command will execute. 0 indicates the top of the hour.
2. Hours (0-23)
The second field determines the hour of execution in a 24-hour format. For example, 14 corresponds to 2 PM.
3. Day of the Month (1-31)
This field indicates the specific day of the month on which the command runs. Using 21 means the 21st day.
4. Month (1-12)
This field specifies the month when the command will execute, where 1 represents January and 12 December.
5. Day of the Week (0-7)
Both 0 and 7 represent Sunday, with numbers 1-6 representing Monday through Saturday.
Practical Examples
Now that we've covered the syntax and terms, let’s explore some practical examples.
Example 1: Every Minute
To schedule a job to run every minute, use:
* * * * * command_to_execute
This runs your command every minute, at all hours, every day.
Example 2: Specific Hour
To run a task at 3 PM every day, your expression would be:
0 15 * * * command_to_execute
Here, the 0 refers to the top of the hour, while 15 specifies 3 PM.
Example 3: Weekdays Only
To execute a task at 6 AM from Monday to Friday:
0 6 * * 1-5 command_to_execute
This means at 6 AM on days 1 through 5 (Monday to Friday).
Example 4: Every 15 Minutes During Business Hours
To execute a task every 15 minutes from 9 AM to 5 PM:
*/15 9-17 * * * command_to_execute
Here, */15 indicates every 15 minutes and 9-17 specifies hours from 9 AM to 5 PM.
Example 5: Specific Day of the Month
To run a job on the 1st of every month at midnight:
0 0 1 * * command_to_execute
This ensures the command runs at 00:00 on the first day of each month.
Example 6: Around New Year’s
To schedule a job to run every day at 11:59 PM from December 31 to January 2:
59 23 31 12 * command_to_execute
59 23 1 1 * command_to_execute
Separate entries are required for each day since December 31 and January 1 fall in different months.
Testing Cron Expressions
Developers might find crafting cron expressions a bit daunting. Thankfully, there are free tools available in-browser, like those on MyAITools, that allow you to test cron expressions quickly. Just enter your expression, and it will provide validation and output options, assisting you in refining your scheduling tasks.
Conclusion
Understanding cron expressions is a vital skill for developers looking to automate tasks effectively. By familiarizing yourself with the syntax and experimenting with various examples, you can ensure smooth, reliable scheduling in your applications. Whether through the command line or web-based tools, automating routines will significantly enhance your productivity and operational efficiency.
Related tools
More blog guides
Frequently asked questions
- What is a cron expression?
- A cron expression is a string consisting of five or six fields that specify the timing for executing scheduled tasks in Unix-like operating systems.
- How do I test a cron expression?
- You can test cron expressions easily with in-browser tools available on sites like MyAITools that validate and demonstrate the scheduling accurately.
- What is the range for the 'Day of the week' field in a cron expression?
- The 'Day of the week' field ranges from 0 to 7, where both 0 and 7 represent Sunday.