What is Cron?
Cron is a time-based job scheduler in Unix-like operating systems. It enables users to schedule commands or scripts to run automatically at specified times, dates, or intervals. The name comes from "chronos," the Greek word for time.
Cron runs as a daemon (background process) and checks every minute whether any scheduled jobs need to run. It's essential for automating system maintenance, backups, monitoring, and countless other recurring tasks.
Cron Expression Syntax
A cron expression consists of five fields (or six, if including seconds) that define when a job should run:
ββββββββββββββ 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 Values
| Field | Range | Special Values |
|---|---|---|
| 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
* (Asterisk)Matches all values. * * * * * runs every minute.
, (Comma)Separates multiple values. 1,15,30 in minute field runs at minutes 1, 15, and 30.
- (Hyphen)Defines a range. 1-5 in day-of-week means Monday through Friday.
/ (Slash)Specifies increments. */15 in minute field means every 15 minutes.
L (Last)Last day of month or week. L in day field means last day of month.
# (Nth)Nth occurrence. 2#1 means first Monday of the month.
Common Examples
# Every minute * * * * * # Every hour at minute 0 0 * * * * # Every day at midnight 0 0 * * * # Every Monday at 9:00 AM 0 9 * * 1 # Every weekday at 8:30 AM 30 8 * * 1-5 # First day of every month at noon 0 12 1 * * # Every 15 minutes */15 * * * * # Every 6 hours 0 */6 * * * # Twice daily at 8 AM and 8 PM 0 8,20 * * * # Every Sunday at 2:30 AM 30 2 * * 0 # Last day of every month at 11 PM 0 23 L * *
Managing Cron Jobs
The crontab Command
# Edit your crontab crontab -e # List your cron jobs crontab -l # Remove all your cron jobs crontab -r # Edit another user's crontab (requires root) sudo crontab -u username -e
System-wide Cron Directories
Many systems have special directories for system cron jobs:
/etc/crontab # System crontab file /etc/cron.d/ # Additional crontab files /etc/cron.hourly/ # Scripts run every hour /etc/cron.daily/ # Scripts run daily /etc/cron.weekly/ # Scripts run weekly /etc/cron.monthly/ # Scripts run monthly
Environment Variables
Cron jobs run with a minimal environment. Common issues arise from missing PATH or other variables:
# Set environment in crontab SHELL=/bin/bash PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin MAILTO=admin@example.com # Then your jobs 0 * * * * /path/to/script.sh
Best Practice: Use Full Paths
# Bad: relies on PATH 0 * * * * python backup.py # Good: uses full paths 0 * * * * /usr/bin/python3 /home/user/scripts/backup.py
Handling Output
By default, cron emails output to the user. You can redirect it:
# Discard all output 0 * * * * /path/to/script.sh > /dev/null 2>&1 # Log to a file 0 * * * * /path/to/script.sh >> /var/log/myjob.log 2>&1 # Log with timestamp 0 * * * * /path/to/script.sh 2>&1 | while read line; do echo "$(date): $line"; done >> /var/log/myjob.log # Email only on error 0 * * * * /path/to/script.sh > /dev/null
Common Use Cases
Database Backups
# Daily MySQL backup at 2 AM 0 2 * * * /usr/bin/mysqldump -u root mydb | gzip > /backups/mydb_$(date +\%Y\%m\%d).sql.gz
Log Rotation
# Compress logs older than 7 days, every Sunday
0 0 * * 0 find /var/log/app -mtime +7 -exec gzip {} \;
Cache Clearing
# Clear cache every 6 hours 0 */6 * * * /usr/bin/php /var/www/app/artisan cache:clear
System Monitoring
# Check disk space every hour, alert if low 0 * * * * /scripts/check-disk.sh | mail -s "Disk Alert" admin@example.com
Scheduled Reports
# Generate weekly report every Monday at 6 AM 0 6 * * 1 /scripts/generate-report.sh && mail -s "Weekly Report" team@example.com < /tmp/report.txt
Troubleshooting Cron
- Check cron is running:
systemctl status cronorservice cron status - Check cron logs:
/var/log/cron,/var/log/syslog, orjournalctl -u cron - Test the command manually: Run the exact command from your crontab in a terminal first.
- Check permissions: Ensure the script is executable (
chmod +x script.sh). - Environment issues: Cron has a minimal environmentβuse full paths.
- Escape percent signs:
%has special meaning in crontab; escape as\%.
Cron Alternatives
While cron is ubiquitous, other schedulers offer additional features:
- systemd timers: Modern Linux alternative with better logging and dependency management.
- anacron: For machines that aren't always runningβruns missed jobs at next boot.
- at: One-time job scheduling instead of recurring jobs.
- Celery Beat: Python-based scheduler for application-level task scheduling.
- Laravel Scheduler: PHP framework's built-in task scheduler using cron as a trigger.
Best Practices
- Always use absolute paths for commands and scripts
- Redirect output appropriately (log or discard)
- Add comments to explain what each job does
- Test commands manually before adding to crontab
- Use lock files to prevent overlapping job runs
- Monitor job execution with logging or external tools
- Set MAILTO for critical jobs to receive error notifications
- Consider time zones when schedulingβcron uses system time
Build Cron Expressions
Use our cron generator to build and validate cron expressions visually.
Open Cron Generator β