Cron Syntax Explained: 5 Fields Decoded
You searched for "Cron Syntax Explained". I know exactly why. You’re staring at a cryptic string of asterisks and numbers, trying to schedule a task that runs every third Tuesday, but only if it’s not a holiday, and also sometimes on alternate Thursdays. The documentation is dense, the examples are too simple or too complex, and you just want to get the darn thing to run without accidentally rebooting your server every minute. It’s a common pain point, and honestly, it’s a wonder more tools don’t make this easier. Let’s demystify this powerful, albeit intimidating, syntax.
The Five Fields: A Minute-by-Minute Breakdown
At its core, cron syntax is a sequence of five fields, separated by spaces. Each field represents a unit of time. Understanding what goes into each field is the key to unlocking cron’s potential. Let’s break them down from left to right:
- Minute (0-59): This is the first field. It specifies the minute of the hour when the command will run. You can use a single number (e.g.,
30for the 30th minute), an asterisk (*) to indicate every minute, or a range (10-20for minutes 10 through 20). Stepped values are also possible, like*/15which means every 15 minutes. - Hour (0-23): The second field dictates the hour of the day. Similar to the minute field, you can use specific hours (e.g.,
9for 9 AM), an asterisk (*) for every hour, or ranges (14-17for hours 2 PM through 5 PM). Stepped values like0-23/2mean every two hours. - Day of Month (1-31): This third field targets the day of the month. Again, you can specify a single day (e.g.,
15for the 15th), use an asterisk (*) for every day of the month, or define a range (1-7for the first week of the month). - Month (1-12 or JAN-DEC): The fourth field specifies the month. You can use numbers (
1for January,12for December), an asterisk (*) for every month, or even three-letter abbreviations (JAN,FEB, etc.). Ranges like3-6cover March through June. - Day of Week (0-7 or SUN-SAT): The final field controls the day of the week. Both
0and7represent Sunday, with1for Monday and so on, up to6for Saturday. An asterisk (*) means every day of the week. You can also use abbreviations likeMON,TUE, etc. Ranges and stepped values are also valid here.
Putting It All Together: Common Scenarios
Let’s look at some practical examples to solidify your understanding. These are the kinds of schedules you’ll likely want to implement:
- Every minute:
* * * * *(Simple, but rarely useful on its own unless you’re testing something!) - Every hour, at the 15-minute mark:
15 * * * * - Every day at 2:30 AM:
30 2 * * * - Every Monday at 9:00 AM:
0 9 * * 1(or0 9 * * MON) - The first day of every month at midnight:
0 0 1 * * - Every weekday (Mon-Fri) at 5:00 PM:
0 17 * * 1-5(or0 17 * * MON-FRI) - Every 10 minutes:
*/10 * * * *
You might also encounter the special string @reboot, which runs a command once after the system boots up. Keep in mind that some cron implementations support additional keywords like @hourly, @daily, @weekly, @monthly, and @yearly, which are essentially shortcuts for common schedules. For instance, @daily is equivalent to 0 0 * * *.
Beyond the Basics: Special Characters and Ranges
The real power of cron syntax comes from its flexibility with special characters:
- Asterisk (
*): Wildcard, matches any value. - Comma (
,): List separator. Use it to specify multiple values (e.g.,1,15,30in the minute field means at minutes 1, 15, and 30). - Hyphen (
-): Range indicator. Use it to specify a range of values (e.g.,9-17in the hour field means hours 9 AM through 5 PM). - Slash (
/): Step values. Use it to specify intervals (e.g.,*/5in the minute field means every 5 minutes). - Question Mark (
?): Primarily used in some cron variations (like Quartz) for the Day of Month or Day of Week fields when you don’t care about that specific field but need to specify the other. In standard Vixie cron, you typically use an asterisk or leave it blank.
Combining these characters allows for incredibly precise scheduling. For example, to run a script every 3 hours on weekdays between 9 AM and 5 PM, you could use 0 */3 * * 1-5. If you're dealing with date and time conversions for your scheduled tasks, our Timestamp Converter tool can be a lifesaver. And for generating unique identifiers that might be part of your scheduled jobs, don't forget the UUID Generator.
However, crafting these complex strings can still be error-prone. That’s where visual tools shine. Instead of wrestling with syntax, you can simply select your desired times and intervals. The OptiPix Cron Builder is designed precisely for this purpose. It’s a free, browser-based tool that generates the correct cron syntax for you visually, without any uploads or account requirements. You get your schedule string instantly, processed entirely in your browser.
Try it free at OptiPix.art.
Try Image Compressor free - your files never leave your device
100% private, offline, no signup - try OptiPix now.
Open Image Compressor