Cron Expressions: A Complete Guide to Scheduling Tasks Like a Pro
By ToolPix Team
What Is a Cron Expression?
A cron expression is a string of five (or six) fields that defines a schedule for automated task execution. Originally part of Unix-like operating systems, cron scheduling is now used everywhere — CI/CD pipelines (GitHub Actions, GitLab CI), cloud functions (AWS Lambda, Google Cloud Functions), Kubernetes CronJobs, database maintenance scripts, and application-level task schedulers. Understanding cron syntax is a fundamental skill for any backend developer, DevOps engineer, or system administrator.
The Five-Field Syntax
A standard cron expression has five fields, separated by spaces:
- Minute (0-59): The minute of the hour when the task runs.
- Hour (0-23): The hour of the day (24-hour format).
- Day of Month (1-31): The day of the month.
- Month (1-12 or JAN-DEC): The month of the year.
- Day of Week (0-7 or SUN-SAT, where both 0 and 7 represent Sunday): The day of the week.
Some systems add a sixth field for seconds (0-59) at the beginning, and some support a seventh field for year. But the five-field format is the universal standard.
Special Characters
Cron expressions use special characters to define complex schedules:
- Asterisk (*): Matches every possible value.
* * * * *runs every minute. - Comma (,): Specifies a list of values.
1,15 * * * *runs at minute 1 and minute 15 of every hour. - Hyphen (-): Defines a range.
0 9-17 * * *runs at the start of every hour from 9 AM to 5 PM. - Slash (/): Defines step values.
*/15 * * * *runs every 15 minutes.0 */2 * * *runs every 2 hours on the hour. - Question mark (?): Used in some systems (like Quartz scheduler) to indicate "no specific value" for day-of-month or day-of-week when the other is specified.
- L: Last.
0 0 L * *runs at midnight on the last day of every month (supported by some schedulers). - W: Nearest weekday.
0 0 15W * *runs on the weekday nearest to the 15th of the month. - Hash (#): Nth weekday.
0 0 * * 5#3runs on the third Friday of every month.
Practical Examples
Here are real-world scheduling scenarios and their cron expressions:
Basic Schedules
- Every day at midnight:
0 0 * * * - Every weekday at 9 AM:
0 9 * * 1-5 - Every Sunday at 2:30 AM:
30 2 * * 0 - First day of every month at noon:
0 12 1 * * - Every 5 minutes:
*/5 * * * *
Advanced Schedules
- Every 30 minutes during business hours on weekdays:
*/30 9-17 * * 1-5 - Quarterly on the 1st at 6 AM:
0 6 1 1,4,7,10 * - Twice daily at 8 AM and 8 PM:
0 8,20 * * * - Every 15 minutes in January only:
*/15 * * 1 * - Weekdays at 9 AM, 12 PM, and 5 PM:
0 9,12,17 * * 1-5
Our Cron Expression Generator lets you build cron expressions visually — select the schedule you want from dropdown menus and the tool generates the correct expression. It also displays the next several execution times so you can verify the schedule is correct before deploying.
Common Mistakes
Even experienced developers make these cron scheduling errors:
1. Forgetting Time Zones
Cron jobs run in the time zone of the server or scheduler, which may differ from your local time. A job scheduled for 0 9 * * * on a UTC server runs at 9 AM UTC — which is 4 AM EST or 1 AM PST. Always verify which time zone your cron scheduler uses.
2. Overlapping Runs
If a task takes longer than the interval between runs, you get overlapping executions. A data sync that takes 20 minutes, scheduled every 15 minutes, will have overlapping processes competing for resources. Use locking mechanisms or ensure the interval exceeds the maximum expected runtime.
3. Day-of-Month and Day-of-Week Conflict
In standard cron, if you specify both day-of-month and day-of-week, the job runs when either condition is met (OR logic), not when both are met (AND logic). 0 0 15 * 5 runs on the 15th of every month AND every Friday — not just on Fridays that fall on the 15th.
4. Using */1 Instead of *
They mean the same thing. */1 (every 1 unit) is equivalent to * (every unit). It is not wrong, but it is unnecessarily verbose and can confuse readers of your configuration.
Combining Cron with Regex for Automation
Cron jobs frequently work together with regular expressions in automation pipelines. A cron job might run a log processing script that uses regex to extract error patterns, or a scheduled task might parse incoming data files using regex-based validation rules.
Example: Automated Log Analysis
A cron job runs every hour to scan application logs for error patterns. The script uses regex like ERROR\s+\[\w+\]\s+(.+) to extract error messages and \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2} to parse timestamps. When critical patterns are detected, it triggers an alert.
Our Regex Tester is the perfect companion for building and validating these patterns. Paste your sample log data, write your regex, and instantly see matches highlighted with capture groups. Like all ToolPix tools, it runs entirely in your browser.
Cron in Modern Infrastructure
Kubernetes CronJobs
Kubernetes uses standard cron syntax in its CronJob resource to schedule containerized tasks. The YAML specification includes a schedule field that accepts a standard five-field cron expression.
GitHub Actions
GitHub Actions supports cron-triggered workflows using the schedule event. The syntax is standard five-field cron, and all times are in UTC. This is useful for nightly builds, scheduled dependency updates, and periodic test runs.
Serverless Functions
AWS EventBridge (formerly CloudWatch Events) and Google Cloud Scheduler use cron expressions to trigger Lambda functions and Cloud Functions on schedule. This pattern is common for data pipelines, report generation, and periodic cleanup tasks.
Build Your Cron Expressions with Confidence
Stop guessing whether your cron expression is correct. Use our Cron Expression Generator to build expressions visually and see exactly when they will fire. Pair it with our Regex Tester for the pattern-matching components of your automated scripts. Both tools run in your browser with no installation required.
Try It Now
Build and decode cron expressions visually. Preview next execution times and use common presets.
Open Cron Expression Generator