Cron Expressions Explained: Syntax, Examples, and Common Patterns
Β· 10 min read
Cron Syntax: The Five Fields
A cron expression has five fields separated by spaces. Each field defines when the job runs:
ββββββββββββββ minute (0-59)
β ββββββββββββββ hour (0-23)
β β ββββββββββββββ day of month (1-31)
β β β ββββββββββββββ month (1-12 or JAN-DEC)
β β β β ββββββββββββββ day of week (0-7, 0 and 7 = Sunday, or SUN-SAT)
β β β β β
* * * * * command
| Field | Range | Special Characters |
|---|---|---|
| Minute | 0-59 | * , - / |
| Hour | 0-23 | * , - / |
| Day of Month | 1-31 | * , - / L W |
| Month | 1-12 | * , - / |
| Day of Week | 0-7 | * , - / L # |
Special Characters
| Character | Meaning | Example |
|---|---|---|
* | Every value | * * * * * = every minute |
, | List of values | 1,15 * * * * = minute 1 and 15 |
- | Range | 0 9-17 * * * = every hour 9am-5pm |
/ | Step/interval | */5 * * * * = every 5 minutes |
L | Last (day of month/week) | 0 0 L * * = last day of month |
W | Nearest weekday | 0 0 15W * * = nearest weekday to 15th |
# | Nth weekday | 0 0 * * 5#3 = 3rd Friday |
Note: L, W, and # are extensions not available in all cron implementations. Standard Unix cron supports *, ,, -, and / only.
Common Patterns
| Expression | Description |
|---|---|
* * * * * | Every minute |
*/5 * * * * | Every 5 minutes |
0 * * * * | Every hour (at minute 0) |
0 */2 * * * | Every 2 hours |
0 0 * * * | Daily at midnight |
0 9 * * * | Daily at 9:00 AM |
0 9 * * 1-5 | Weekdays at 9:00 AM |
0 0 * * 0 | Weekly on Sunday at midnight |
0 0 1 * * | First day of every month |
0 0 1 1 * | January 1st at midnight (yearly) |
30 4 * * 1 | Monday at 4:30 AM |
0 0 * * 6,0 | Weekends at midnight |
0 8-17 * * 1-5 | Every hour during business hours |
0 0 1,15 * * | 1st and 15th of each month |
*/10 * * * * | Every 10 minutes |
Build and test expressions with our Cron Expression Generator or parse existing ones with Cron Expression Parser.
Managing Crontab
# Edit your crontab
crontab -e
# List current crontab entries
crontab -l
# Remove all crontab entries (careful!)
crontab -r
# Edit another user's crontab (root only)
crontab -u username -e
# Example crontab file
SHELL=/bin/bash
PATH=/usr/local/bin:/usr/bin:/bin
[email protected]
# Backup database daily at 2 AM
0 2 * * * /home/user/scripts/backup.sh >> /var/log/backup.log 2>&1
# Clean temp files every Sunday at 3 AM
0 3 * * 0 find /tmp -mtime +7 -delete
# Health check every 5 minutes
*/5 * * * * curl -s https://example.com/health > /dev/null
Crontab Tips
- Always redirect output:
>> /var/log/job.log 2>&1β otherwise cron emails output to MAILTO - Use full paths for commands β cron has a minimal PATH
- Set SHELL and PATH at the top of your crontab
- Test your command manually before adding to crontab
- Use
flockto prevent overlapping runs:flock -n /tmp/job.lock /path/to/script.sh
Common Pitfalls
- Environment variables β Cron runs with a minimal environment. Your script may work in terminal but fail in cron because PATH, HOME, or other variables are different. Always use full paths or set variables explicitly.
- Timezone confusion β Cron uses the system timezone by default. Set
CRON_TZin your crontab if needed. - Day of month AND day of week β When both are set (not *), cron runs when EITHER matches, not both.
0 0 1 * 5runs on the 1st AND every Friday, not "the 1st if it's a Friday." - Percent signs β In crontab,
%is a newline character. Escape with\%or put the command in a script. - No seconds field β Standard cron minimum is 1 minute. For sub-minute scheduling, use a wrapper script with sleep.
Alternatives to Cron
| Tool | Advantage | Use Case |
|---|---|---|
| systemd timers | Better logging, dependencies, randomized delay | Modern Linux systems |
| at | One-time scheduled execution | Run once at a specific time |
| anacron | Runs missed jobs after downtime | Laptops, machines that sleep |
| node-cron | In-process scheduling for Node.js | Application-level scheduling |
| Celery Beat | Distributed task scheduling | Python/Django applications |
| GitHub Actions | Cloud-based, cron syntax supported | CI/CD pipelines |
Frequently Asked Questions
What does * mean in cron?
The asterisk (*) means "every" β it matches all possible values for that field. * in the minute field means every minute, * in the hour field means every hour, and so on.
How do I run a cron job every 5 minutes?
Use */5 * * * * β the */5 in the minute field means "every 5th minute" (0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55).
What is the difference between cron and crontab?
Cron is the daemon (background service) that executes scheduled tasks. Crontab (cron table) is the configuration file that lists the schedule entries. You edit the crontab to tell cron what to run and when.
Can cron run every second?
No. Standard cron's minimum interval is 1 minute. For sub-minute scheduling, use a loop with sleep in a script, systemd timers with OnUnitActiveSec, or specialized tools like fcron.
What timezone does cron use?
By default, cron uses the system timezone. You can override this by setting CRON_TZ at the top of your crontab file, e.g., CRON_TZ=America/New_York.