keepcalculator

06 Numbers and Powers

How to Read a Cron Expression: Fields & Operators

Card No. 06·6379 · Updated · Standard formula, shown below

Cron expressions are compact strings that schedule jobs on Unix-like systems, from simple daily backups to complex recurring tasks. This guide breaks down each of the five standard fields, explains the operators you can use, and shows you real examples so you can write and read cron expressions with confidence. No prior knowledge is needed—just a basic understanding of time units.

What You’ll Need

  • A basic familiarity with command-line concepts (not required, but helpful for testing).
  • Access to a system with cron installed (Linux, macOS, or Windows Subsystem for Linux) if you want to test expressions.
  • This reference: you can bookmark it for quick lookup.

Step-by-Step

  1. Understand the five fields. A standard cron expression has five fields separated by spaces, in this order: minute, hour, day of month, month, day of week. For example, 30 2 * * * means “at 2:30 AM every day.�
  2. Learn the allowed values for each field.
    – Minute: 0–59
    – Hour: 0–23 (0 is midnight)
    – Day of month: 1–31
    – Month: 1–12 (or JAN–DEC)
    – Day of week: 0–7 (0 and 7 both mean Sunday, or SUN–SAT)
  3. Use the asterisk (*) for “every�. An asterisk in a field means “every possible value.� So * * * * * runs every minute, every hour, every day, every month, every weekday.
  4. Use commas (,) for lists. To specify multiple values, separate them with commas. For example, 0 9,17 * * * runs at 9:00 AM and 5:00 PM every day.
  5. Use hyphens (-) for ranges. A hyphen defines a range. 0 9-17 * * * runs at the top of every hour from 9 AM to 5 PM inclusive (9,10,11,…,17).
  6. Use the slash (/) for step values. A slash after a range or asterisk means “every N units.� For example, */15 * * * * runs every 15 minutes (0,15,30,45). 0 0-6/2 * * * runs every two hours from midnight to 6 AM (0,2,4,6).
  7. Combine operators as needed. You can mix lists, ranges, and steps within a single field. For example, 0 9,13-14 * * 1-5 runs at 9 AM, 1 PM, and 2 PM on weekdays.
  8. Understand the day-of-week special rule. If both day-of-month and day-of-week are restricted (not *), the job runs when either matches. For example, 0 0 1 * 1 runs on the 1st of the month AND on every Monday.
  9. Use names for months and days (optional). You can use three-letter abbreviations: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC; and SUN, MON, TUE, WED, THU, FRI, SAT. These are case-insensitive.
  10. Read an expression from left to right. Start with the minute, then hour, then day-of-month, month, and day-of-week. For example, 15 14 1 * * means “at 2:15 PM on the 1st of every month.�
  11. Test your expression. Use crontab -e to edit your user’s crontab and crontab -l to list it. You can also use online cron testers or the cron daemon’s logs to verify.

Common Mistakes

  • Confusing day-of-week 0 and 7: Both are Sunday, but some systems treat 7 as Sunday only. Stick to 0 for Sunday to avoid confusion.
  • Using a range that crosses midnight: 22-2 is invalid. You must split it into two entries: 22-23,0-2.
  • Forgetting that day-of-week and day-of-month are OR when both are set: This often causes unexpected runs. If you want a job on the 1st AND on Mondays, you need two separate cron lines.
  • Using step values incorrectly: */2 in the hour field runs every 2 hours, but 0-6/2 runs at 0,2,4,6 (not 0-6 with a step of 2, which would be 0,2,4,6). That’s correct, but note that 1-6/2 runs at 1,3,5 (not 1,3,5,7).
  • Ignoring timezone: Cron uses the system’s local time. If your server is in UTC, your job will run at UTC time, not your local time.

Tips

  • Start with a simple expression and test. Use * * * * * to see if your command runs every minute, then adjust.
  • Use comments in your crontab. Lines starting with # are ignored, so you can annotate each job.
  • Redirect output to a log file. Add >> /path/to/log 2>&1 to capture errors and output.
  • Use the special strings if your system supports them: @reboot, @daily, @hourly, @weekly, @monthly, @yearly. These are easier to read.
  • Double-check the day-of-week rule. Many people expect AND logic, but cron uses OR. When in doubt, test with a short interval.

Troubleshooting

  • Job doesn’t run at all: Check the cron service is running (systemctl status cron on most Linux). Verify the syntax with crontab -e (it will reject invalid lines). Check the command itself—try running it manually.
  • Job runs at the wrong time: Check the system timezone with date. Also verify you didn’t swap hour and minute fields (common mistake).
  • Job runs more often than expected: Check if you have both day-of-month and day-of-week set. Remember, that’s an OR condition. Also check for duplicate cron entries.
  • Job runs but fails: Look at the cron log (usually /var/log/syslog or /var/log/cron) and your command’s output. Redirect output to a file to see errors.
  • Step values not working as expected: For example, 1-10/3 runs at 1,4,7,10 (not 1,4,7,10,13). The step starts at the beginning of the range and increments.

Frequently Asked Questions

What does */5 * * * * mean?

It runs every 5 minutes, at minute 0,5,10,15,…,55 of every hour, every day.

How do I run a job every weekday at 9 AM?

Use 0 9 * * 1-5. The 1-5 covers Monday to Friday.

Can I use both day-of-month and day-of-week?

Yes, but remember they are OR’ed. If you need AND logic, you must add a condition inside your command (e.g., check the date) or use two separate lines.

What does 0 0 1 1 * mean?

It runs at midnight on January 1st every year.

Is 0 0 * * 0 the same as 0 0 * * 7?

Yes, both run at midnight on Sunday (0 and 7 are both Sunday). However, some cron implementations treat 7 as Sunday only, so 0 is safer.

How do I run a job every 30 seconds?

Standard cron only supports minute-level granularity. You can use a script that sleeps 30 seconds and runs twice, or use a systemd timer with OnUnitActiveSec=30s.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →