About Cron Expression Generator
Cron is a time-based job scheduler widely used in Unix-like systems (Linux, macOS, etc.). It allows users to set scheduled tasks that automatically execute commands or scripts at specified times, dates, or intervals.
This tool helps you intuitively generate Cron expressions without needing to memorize complex syntax rules. You can create various scheduled tasks through simple clicking and input.
Cron Expression Format
A standard Cron expression consists of 5 or 6 fields, separated by spaces:
minute hour day month weekday [year]
- Minute (0-59): Specifies which minute to execute the task
- Hour (0-23): Specifies which hour to execute the task (24-hour format)
- Day (1-31): Specifies which day of the month to execute the task
- Month (1-12): Specifies which month to execute the task, can also use English abbreviations (JAN, FEB, etc.)
- Weekday (0-6): Specifies which day of the week to execute the task, 0 or 7 represents Sunday, can also use English abbreviations (SUN, MON, etc.)
- Year (optional, 1970-2099): Some systems support the year field (like Spring's Quartz)
Special Characters Explained
- * (asterisk): Means "every". For example: * * * * * means execute every minute
- , (comma): Lists multiple values. For example: 1,3,5 means execute at minutes 1, 3, and 5
- - (hyphen): Indicates a range. For example: 1-5 means from minute 1 to 5, i.e., execute at 1, 2, 3, 4, and 5
- / (slash): Indicates step (interval). For example: */5 means execute every 5 times; 10/5 means execute every 5 times starting from the 10th
- ? (question mark): Only used in "day" and "weekday" fields to indicate unspecified value, avoiding conflicts. For example: when specifying "weekday", use ? for "day"; when specifying "day", use ? for "weekday"
- L: Means "last". For example: in the "day" field, L means the last day of the month; in the "weekday" field, 6L means the last Friday of the month
- W: Means "weekday" (Monday to Friday). For example: 15W means the nearest weekday to the 15th of the month
- #: Means "nth". For example: 6#3 means the 3rd Friday of the month
Common Examples
0 * * * *- Execute once per hour (at minute 0 of each hour)*/30 * * * *- Execute every 30 minutes0 0 * * *- Execute at midnight every day (once per day)0 9 * * *- Execute at 9 AM every day0 9-17 * * *- Execute once per hour from 9 AM to 5 PM every day0 0 * * 0- Execute at midnight every Sunday0 0 * * 1-5- Execute at midnight Monday to Friday every day0 0 1 * *- Execute at midnight on the 1st of every month0 0 1 1 *- Execute at midnight on January 1st every year (New Year's Day)0 */2 * * *- Execute every 2 hours0 0 12 1 *- Execute at noon on the 1st of every month0 0 12 * *- Execute at noon every day0 15 10 * *- Execute at 10:15 AM every day0 0,12,18 * * *- Execute at 0:00, 12:00, and 18:00 every day (three times daily)0 10,14,16 * * *- Execute at 10 AM, 2 PM, and 4 PM every day
Practical Use Cases
- Data Backup: 0 2 * * * - Automatically backup database at 2 AM every day
- Log Cleanup: 0 3 * * 0 - Clean up expired logs at 3 AM every Sunday
- Report Generation: 0 8 * * 1-5 - Generate daily reports at 8 AM on weekdays
- System Monitoring: */5 * * * * - Check system status every 5 minutes
- Email Sending: 0 9 * * 1 - Send weekly report email at 9 AM every Monday
- Cache Update: 0 */4 * * * - Update cache every 4 hours
- Scheduled Scraping: 0 1 * * * - Scrape data at 1 AM every day
Frequently Asked Questions
Q: Can "day" and "weekday" be specified simultaneously?
A: No. "Day" and "weekday" are mutually exclusive; one must use a specific value or *, and the other must use ? to indicate unspecified. For example: use 0 0 1 * ? for the 1st of every month; use 0 0 ? * 0 for every Sunday.
Q: What's the difference between L and W?
A: L (Last) means the last one, e.g., 6L means the last Friday; W (Weekday) means the nearest weekday, e.g., 15W means the nearest weekday around the 15th.
Q: How to set the last weekday of each month?
A: Use the combination: LW means the last weekday of each month. Or implement with conditional logic.
Q: What's the difference between 5-digit and 6-digit Cron expressions?
A: Standard 5-digit Cron: minute hour day month weekday; 6-digit Cron (like Quartz) adds a second or year field at the beginning or end. Spring Boot's @Scheduled supports 6 digits by default (second minute hour day month weekday).
Q: How to debug Cron expressions?
A: You can use online tools (like crontab.guru) to see expression explanations, or test with small time intervals (like */1) to confirm correctness before changing to production time settings.