Cron is the time-based job scheduler used in Unix-like operating systems to automate repetitive tasks. From log rotation and database backups to deployment scripts and health checks, cron jobs are ubiquitous in system administration and DevOps. This guide covers the syntax, provides ready-to-use examples, and helps you avoid common pitfalls.
A standard cron expression consists of five fields separated by spaces:
* * * * *
| | | | |
| | | | +-- Day of Week (0-7, 0 and 7 = Sunday)
| | | +-------- Month (1-12)
| | +-------------- Day of Month (1-31)
| +-------------------- Hour (0-23)
+-------------------------- Minute (0-59)
| Character | Meaning | Example |
|---|---|---|
* | Any value | * * * * * = every minute |
, | List of values | 1,15 * * * * = minute 1 and 15 |
- | Range | 1-5 * * * * = minutes 1 through 5 |
/ | Step/interval | */5 * * * * = every 5 minutes |
| Expression | Description |
|---|---|
* * * * * | Every minute |
*/5 * * * * | Every 5 minutes |
*/15 * * * * | Every 15 minutes |
0 * * * * | Every hour (at minute 0) |
0 */6 * * * | Every 6 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 | Weekly on Monday at midnight |
0 0 1 * * | First day of every month at midnight |
0 0 1 1 * | January 1st at midnight (yearly) |
30 2 * * * | Daily at 2:30 AM |
0 0 15 * * | 15th of every month at midnight |
0 8-17 * * 1-5 | Every hour, 8AM-5PM, Mon-Fri |
Many cron implementations support these convenient shortcuts:
| Shortcut | Equivalent | Description |
|---|---|---|
@yearly | 0 0 1 1 * | Once a year |
@monthly | 0 0 1 * * | Once a month |
@weekly | 0 0 * * 0 | Once a week |
@daily | 0 0 * * * | Once a day |
@hourly | 0 * * * * | Once an hour |
@reboot | N/A | Run once at startup |
0 2 * * * /usr/local/bin/backup-db.sh >> /var/log/backup.log 2>&1
0 0,12 * * * certbot renew --quiet
Monitor your certificate expiry with the SSL Certificate Checker.
0 0 * * 0 /usr/sbin/logrotate /etc/logrotate.conf
*/5 * * * * curl -sf https://devtoolkit.ink/api/v1/health || alert.sh
0 3 * * * find /tmp -type f -mtime +7 -delete
# Edit your crontab
crontab -e
# List current cron jobs
crontab -l
# Remove all cron jobs (careful!)
crontab -r
# Edit another user's crontab (as root)
crontab -u username -e
# System-wide cron jobs
ls /etc/cron.d/
ls /etc/cron.daily/
ls /etc/cron.hourly/
Cron runs with a minimal environment. Commands that work in your shell may fail in cron because /usr/local/bin is not in the cron PATH.
# Fix: use absolute paths
0 * * * * /usr/local/bin/node /home/user/script.js
# Or set PATH at the top of your crontab
PATH=/usr/local/bin:/usr/bin:/bin
0 * * * * node /home/user/script.js
By default, cron emails output to the user. If mail is not configured, output is lost. Always redirect to a log file:
# Redirect stdout and stderr to a log file
0 * * * * /scripts/task.sh >> /var/log/task.log 2>&1
# Discard output entirely
0 * * * * /scripts/task.sh > /dev/null 2>&1
Cron uses the system timezone by default. If your server is in UTC but you expect local time, jobs run at unexpected hours. Check with timedatectl and convert timestamps using the Unix Timestamp Converter.
When both day-of-month and day-of-week are specified (not *), cron runs the job when either condition is true, not both. This catches many people off guard:
# Runs on the 15th AND every Friday (not just Friday the 15th)
0 0 15 * 5 /scripts/task.sh
# To run only on Friday the 15th, use a wrapper:
0 0 15 * * [ "$(date +\%u)" = "5" ] && /scripts/task.sh
If a job takes longer than the interval between runs, multiple instances stack up. Use flock to prevent overlap:
*/5 * * * * flock -n /tmp/task.lock /scripts/long-task.sh
Standard Unix cron uses 5 fields (minute through day-of-week). Some systems like Quartz, Spring, and AWS add a 6th field for seconds. The DevToolKit Cron Parser supports the standard 5-field format.
Cron's minimum resolution is 1 minute. For sub-minute scheduling, use two cron entries offset by 30 seconds, or switch to systemd timers or a dedicated scheduler.
Check /var/log/syslog or /var/log/cron for execution records. Verify permissions, PATH, and ensure the script is executable (chmod 755).
DevToolKit — 42+ Free Online Developer Tools