First, Understand the Structure
A cron expression is 5 fields separated by spaces:
┌─ minute (0-59)
│ ┌─ hour (0-23)
│ │ ┌─ day of month (1-31)
│ │ │ ┌─ month (1-12)
│ │ │ │ ┌─ day of week (0-6, 0=Sunday)
│ │ │ │ │
* * * * *
The core idea when writing cron: whichever field you want to restrict, fill in a specific value; if there’s no restriction, use *.
The Four Key Symbols
*— every value. Placed in a field, it means “no restriction on this dimension.”,— enumeration. E.g.9,12,18in the hour field.-— range. E.g.1-5in the day-of-week field (Monday through Friday)./— step. E.g.*/15in the minute field (every 15 minutes).
Building It Step by Step
Goal 1: run every 5 minutes. Just restrict the minute dimension to “every 5 minutes” and leave the rest open:
*/5 * * * *
Goal 2: run at 3 AM every day. Restrict minute=0 and hour=3, leave the rest open:
0 3 * * *
Goal 3: run at 9 AM every Monday. minute=0, hour=9, day of week=1:
0 9 * * 1
Goal 4: run on the hour, every hour, on weekdays. minute=0, day of week=1-5:
0 * * * 1-5
The pattern: from left to right, fill in specific values for the dimensions you want to fix, and leave the rest as *. Use / for “every N,” , for “several specific values,” and - for “a range.”
Adding It to crontab
Once you’ve written the expression, pair it with the command to run and add it to your crontab:
# After crontab -e, add a line:
0 3 * * * /usr/bin/backup.sh >> /var/log/backup.log 2>&1
- The first part is “when” (the expression), and the second part is “what to run” (the command).
- It’s recommended to use absolute paths for commands, and to redirect output to a log for easier troubleshooting.
- After saving and exiting, run
crontab -lto confirm it was added.
In the Cron Expression Generator, fill in your command and you get this entire crontab line ready to copy; it also shows the equivalent syntax for Windows Task Scheduler, Node.js, Python, Spring, and other environments.
Verifying Your Expression
The most common beginner mistake is “thinking it runs daily when it actually runs every minute.” Always verify after writing:
- Open the Cron Expression Generator;
- Paste your expression;
- Check whether the plain-language explanation and the next 5 run times match your intent.
Keep Learning
- Not clear on what the fields and symbols mean? Read What Is Cron.
- Just want to copy common patterns? See Cron Expression Examples.
- Wrote it but it won’t run? See What to Do When crontab Won’t Run.