How to Write a Cron Expression
Learn how to write a cron expression correctly by avoiding common field count mistakes, syntax errors, and scheduling conflicts.
Writing a cron expression involves constructing a string of space-separated fields that define a specific schedule for recurring system tasks and background jobs. To write an accurate expression, you must combine time units—such as minutes, hours, days, and months—alongside specialized operators like asterisks, ranges, and increments while adhering strictly to your environment’s expected field structure.
The Most Common Cron Expression Mistake and Its Output
The single most frequent mistake engineers make when writing a cron expression is confusing standard Unix cron format with extended scheduler formats, such as those used by Quartz or Java-based triggers. Standard Unix-like operating systems expect a string comprised of five mandatory time fields followed by a shell command: minute, hour, day-of-month, month, and day-of-week. Conversely, extended schedulers often require six or seven fields, prepending a seconds field and appending an optional year field.
When a developer writes a standard 5-field expression—such as 10 14 * * 1—intending to run a job at 10 seconds past 2:00 PM on Mondays, standard Unix cron interprets the first field as 10 minutes past the hour instead of seconds. As a result, the job fires at 2:10 PM instead of the intended second, silently altering execution behavior without throwing a syntax error. Alternatively, if a 7-field expression is pasted directly into a standard Unix crontab file, cron treats the extra fields as part of the shell command or flags an unrecognized token error, causing the entire job entry to fail entirely.
Another frequent pitfall involving the first mistake is mishandling numeric boundaries. Each time unit enforces strict numeric limits: minutes range from 0 to 59, hours range from 0 to 23, days of the month range from 1 to 31, months range from 1 to 12 (or JAN-DEC), and days of the week range from 0 to 6 (where 0 or 7 represents Sunday depending on the system). Providing a value outside these boundaries—such as specifying 60 in the minute field or 24 in the hour field—results in invalid schedule parsing where the cron daemon refuses to execute the task.
The Second Mistake: Overlooking Field Restrictions and Day Conflicts
The second most common error is mismanaging the interaction between the day-of-month and day-of-week fields, or misusing increment operators. In traditional Unix environments, when both the day-of-month and day-of-week fields are restricted—meaning they contain explicit values or ranges rather than wildcards or question marks—the execution logic changes drastically. Instead of requiring both fields to match, the system executes the job if either field matches the current date.
In extended scheduling frameworks, using a wildcard or a specific value incorrectly alongside the question mark symbol (?) can trigger configuration rejections. The question mark is exclusively permitted in the day-of-month and day-of-week fields to denote an arbitrary value when the other field is already constrained. Placing a question mark in any other field, or attempting to use both a wildcard and a question mark simultaneously for both day fields, violates syntax rules and prevents the trigger from registering.
Additionally, developers often misuse the increment operator (/). For example, writing an increment without a proper base value or starting point—such as incorrectly formatting step values—can lead to unexpected firing intervals. In cron syntax, an expression like an increment specifies the amount by which to increment values, meaning a step applied improperly across an incorrect range will either repeat every single cycle or skip necessary intervals.
The Correct Method for Constructing Cron Expressions
To construct a valid cron expression without falling into formatting traps, follow a systematic, step-by-step derivation method that builds the string from the smallest time denomination outward.
First, identify your target environment. If you are writing a schedule for a standard Linux crontab file managed via command-line tools, structure your expression using five time fields: minute, hour, day-of-month, month, and day-of-week. If you are configuring an enterprise Java application or Quartz-based trigger, use the extended six or seven fields starting with seconds.
Second, determine your execution frequency and map each component to its precise numerical boundary:
- For the minute field, select a value between 0 and 59.
- For the hour field, select a value between 0 and 23 in 24-hour time format.
- For the day-of-month field, select a value between 1 and 31.
- For the month field, select 1 to 12 or use standard abbreviations like JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC.
- For the day-of-week field, select 0 to 6 (where Sunday is 0 or 6 depending on system implementation).
Third, apply special characters purposefully:
- Use the asterisk (
*) to represent all values for a given time unit. - Use the hyphen (
-) to define a continuous range of values, such as specifying hours or minutes consecutively. - Use the comma (
,) to specify multiple discrete values within a single field, allowing a job to trigger at multiple specific intervals. - Use the slash (
/) to specify increments or step values.
For example, suppose you want to schedule a database synchronization script to run every 10 minutes between the hours of 8:00 AM and 5:00 PM (17:00), exclusively on weekdays (Monday through Friday), in a standard 5-field Unix environment.
- Minute field: You want it to run every 10 minutes, starting at zero. This translates to
*/10. - Hour field: You want it restricted to hours 8 through 17. This translates to range syntax
8-17. - Day-of-month field: Since it runs on any day of the month as long as it is a weekday, you place a wildcard or use an arbitrary placeholder depending on parser requirements, or simply use
*. - Month field: It runs every month, so use
*. - Day-of-week field: It runs Monday through Friday, which can be expressed as a range or list of weekdays (e.g.,
1-5).
Assembling these fields produces the complete cron schedule string followed by your shell command:
*/10 8-17 * * 1-5 /usr/local/bin/sync_database.sh
For systems where manual construction is cumbersome or prone to translation errors between Unix and cloud infrastructure formats, you can use the free Cron to AWS EventBridge Cron Converter to accurately map your scheduling expressions without manual syntax restructuring.
Verifying Your Schedule with a Quick Check
Before deploying your cron expression to a production environment, perform a quick manual validation check to ensure the output matches your expectations and avoids silent failures.
- Count your fields: Verify that your expression has exactly five fields for standard Unix systems, or six/seven fields for extended schedulers. Extra or missing whitespace separators will misalign every subsequent time parameter.
- Check numeric ranges: Confirm that every integer falls strictly within its permitted boundary (minutes 0-59, hours 0-23, days 1-31, months 1-12, weekdays 0-6).
- Inspect conflicting day fields: Ensure that if both day-of-month and day-of-week fields contain restrictive parameters, you understand that matching either condition will trigger the job, or adjust one field to a wildcard or question mark if exclusivity is required.
- Test special strings if applicable: If your environment supports predefined shorthand shortcuts—such as
@hourly,@daily,@midnight,@weekly,@monthly, or@yearly—verify that they substitute correctly for their long-form equivalent fields before relying on them for critical automated workflows.
Open the Cron to AWS EventBridge Cron Converter