Cron Job Syntax: The Complete Guide with Examples
Cron syntax is something every developer looks up every single time. This guide explains every field, special character and common schedule β so you never have to Google it again.
What Is a Cron Job?
A cron job is a scheduled task that runs automatically at specified times on Unix/Linux systems. The name comes from Chronos (Greek god of time). Cron jobs are used for:
- Running database backups every night
- Sending scheduled emails or reports
- Clearing cache or temp files periodically
- Syncing data between systems
- Running health checks and monitoring scripts
π Don't want to memorize the syntax? Use our free Cron Builder β build expressions visually and see exactly when your job will run next.
Cron Expression Structure
A cron expression has 5 fields separated by spaces:
ββββββββββββββ minute (0β59)
β ββββββββββββββ hour (0β23)
β β ββββββββββββββ day of month (1β31)
β β β ββββββββββββββ month (1β12)
β β β β ββββββββββββββ day of week (0β6, Sunday=0)
β β β β β
* * * * * command to execute
| Field | Range | Special characters |
|---|---|---|
| Minute | 0β59 | * , - / |
| Hour | 0β23 | * , - / |
| Day of month | 1β31 | * , - / ? L W |
| Month | 1β12 or JANβDEC | * , - / |
| Day of week | 0β6 or SUNβSAT | * , - / ? L # |
Special Characters Explained
* β Asterisk (every value)
# Run every minute
* * * * *
# Run every hour at minute 0
0 * * * *
, β Comma (list of values)
# Run at 9 AM and 5 PM every day
0 9,17 * * *
# Run on Monday, Wednesday and Friday
0 9 * * 1,3,5
- β Hyphen (range of values)
# Run every hour from 9 AM to 5 PM
0 9-17 * * *
# Run Monday through Friday at 8 AM
0 8 * * 1-5
/ β Slash (step values)
# Run every 15 minutes
*/15 * * * *
# Run every 6 hours
0 */6 * * *
# Run every 2 days
0 0 */2 * *
? β Question mark (no specific value β day fields only)
# Run on the 15th of every month, any day of week
0 0 15 * ?
# Run every Monday, any day of month
0 0 ? * 1
L β Last (last day of month or week)
# Run on the last day of every month
0 0 L * *
# Run on the last Friday of every month
0 0 ? * 5L
Most Common Cron Schedules
| Expression | Meaning |
|---|---|
* * * * * | Every minute |
*/5 * * * * | Every 5 minutes |
*/15 * * * * | Every 15 minutes |
*/30 * * * * | Every 30 minutes |
0 * * * * | Every hour (at :00) |
0 */2 * * * | Every 2 hours |
0 0 * * * | Every day at midnight |
0 9 * * * | Every day at 9 AM |
0 9 * * 1-5 | Weekdays at 9 AM |
0 0 * * 0 | Every Sunday at midnight |
0 0 1 * * | First day of every month |
0 0 L * * | Last day of every month |
0 0 1 1 * | Every year on January 1st |
0 9,17 * * 1-5 | Weekdays at 9 AM and 5 PM |
0 0 * * 1 | Every Monday at midnight |
Cron Shorthand Strings
Most cron implementations support these convenient shortcuts:
| Shorthand | Equivalent | Meaning |
|---|---|---|
@yearly | 0 0 1 1 * | Once a year, January 1st |
@monthly | 0 0 1 * * | Once a month, first day |
@weekly | 0 0 * * 0 | Once a week, Sunday midnight |
@daily | 0 0 * * * | Once a day, midnight |
@hourly | 0 * * * * | Once an hour |
@reboot | N/A | Once at startup |
Real-World Cron Job Examples
Database backup every night at 2 AM:
0 2 * * * /usr/bin/mysqldump -u root mydb > /backups/db-$(date +%Y%m%d).sql
Clear temp files every Sunday at 3 AM:
0 3 * * 0 rm -rf /tmp/myapp/*
Send weekly report every Monday at 9 AM:
0 9 * * 1 /usr/bin/python3 /scripts/weekly-report.py
Check server health every 5 minutes:
*/5 * * * * /scripts/health-check.sh >> /var/log/health.log 2>&1
Renew SSL certificate on the 1st of every month:
0 0 1 * * certbot renew --quiet
Run a Node.js script every weekday at 8 AM:
0 8 * * 1-5 cd /app && /usr/bin/node /app/scripts/daily-sync.js
How to Edit Crontab
# Open crontab editor
crontab -e
# List all cron jobs for current user
crontab -l
# Remove all cron jobs
crontab -r
# Edit crontab for a specific user (root only)
crontab -u username -e
Cron Job Logging
By default cron sends output to your email. To redirect output to a log file:
# Redirect stdout and stderr to a log file
*/30 * * * * /scripts/my-script.sh >> /var/log/my-script.log 2>&1
# Suppress all output (silent)
*/30 * * * * /scripts/my-script.sh > /dev/null 2>&1
# Log with timestamp
*/30 * * * * echo "$(date): running" >> /var/log/my-script.log && /scripts/my-script.sh >> /var/log/my-script.log 2>&1
Common Cron Mistakes to Avoid
- Wrong timezone β cron uses the system timezone. Always check with
datecommand before relying on time-sensitive jobs. - Using relative paths β cron runs with a minimal environment. Always use absolute paths for commands and files.
- No logging β add
>> /var/log/myjob.log 2>&1to every job so you can debug failures. - Day of month vs day of week conflict β if both are set, cron runs when EITHER condition is true (OR, not AND).
- Forgetting environment variables β cron doesn't load your shell profile. Set PATH explicitly in crontab.
Set PATH in Crontab
# Add this at the top of your crontab
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# Then your jobs
0 2 * * * backup.sh
Cron vs Other Schedulers
| Scheduler | Best for | Complexity |
|---|---|---|
| Cron | Simple recurring tasks on Linux/Mac | Low |
| Systemd timers | Modern Linux systems, more control | Medium |
| Task Scheduler | Windows environments | Low |
| Celery Beat | Python apps with distributed tasks | High |
| node-cron | Node.js applications | Low |
| Quartz | Java applications | Medium |
Frequently Asked Questions
How do I run a cron job every 10 seconds?
Standard cron only goes down to 1 minute. To run every 10 seconds use:
* * * * * /script.sh
* * * * * sleep 10; /script.sh
* * * * * sleep 20; /script.sh
* * * * * sleep 30; /script.sh
* * * * * sleep 40; /script.sh
* * * * * sleep 50; /script.sh
What does */5 mean in cron?
The / means "every" and */5 means "every 5 units". In the minute field, */5 means every 5 minutes (at 0, 5, 10, 15... 55). In the hour field, */5 means every 5 hours.
How do I test a cron expression?
Use our free Cron Builder β paste any expression and see the next 10 run times instantly. No guessing required.
Does cron run if the computer is off?
No β if the system is off when a job was scheduled, it won't run when it comes back online (unless you use anacron which handles missed jobs).
What is the difference between crontab and cron?
cron is the daemon (background service) that runs scheduled jobs. crontab is the file containing your schedule, and also the command used to edit that file.
How do I run a cron job on weekdays only?
# Run at 9 AM Monday through Friday
0 9 * * 1-5
Use our Cron Builder to create and validate cron expressions. See exactly when your job will run next β no syntax memorization needed.
Cron Builder β