Cron is the Linux task scheduler. It runs commands at specified times — every minute, daily, weekly, or on a custom schedule. System backups, log rotations, cleanup scripts, monitoring checks — if it needs to run automatically, cron is how you do it. This guide covers crontab syntax, common patterns, and practical examples.
How Cron Works
The crond daemon runs in the background and checks crontab files every minute. Each user can have their own crontab, and there is also a system-wide crontab at /etc/crontab.
systemctl status cron # Ubuntu/Debian
systemctl status crond # RHEL/Rocky/Fedora
crontab Syntax
Each cron entry has five time fields followed by the command:
# .---------- minute (0-59)
# | .-------- hour (0-23)
# | | .------ day of month (1-31)
# | | | .---- month (1-12)
# | | | | .-- day of week (0=Sun, 6=Sat, 7=Sun)
# | | | | |
# * * * * * command
Special values:
*— every value (every minute, every hour, etc.)*/5— every 5 (every 5 minutes)1,15— at minute 1 and minute 151-5— range: Monday through Friday (for day-of-week)
Common Cron Schedule Examples
# Every minute
* * * * * /usr/local/bin/healthcheck.sh
# Every day at 2:30 AM
30 2 * * * /usr/local/bin/backup.sh
# Every Monday at 6 AM
0 6 * * 1 /usr/local/bin/weekly-report.sh
# Every 15 minutes
*/15 * * * * /usr/local/bin/sync.sh
# First day of every month at midnight
0 0 1 * * /usr/local/bin/monthly-cleanup.sh
# Every weekday (Mon-Fri) at 8 AM
0 8 * * 1-5 /usr/local/bin/morning-check.sh
# Twice a day at 6 AM and 6 PM
0 6,18 * * * /usr/local/bin/update-cache.sh
Editing Your Crontab
crontab -e # Edit your crontab (opens in $EDITOR)
crontab -l # List your current crontab
crontab -r # Remove your crontab (careful — no confirmation)
crontab -u alok -l # List crontab for user alok (as root)
On first use, you may be asked to choose an editor. Set your preferred editor:
export EDITOR=nano
crontab -e
System Crontab Files
# System crontab (has an extra "user" field)
cat /etc/crontab
# Drop-in directories for scripts
ls /etc/cron.d/ # Custom cron files (with user field)
ls /etc/cron.daily/ # Scripts run daily by anacron
ls /etc/cron.weekly/ # Scripts run weekly
ls /etc/cron.monthly/ # Scripts run monthly
ls /etc/cron.hourly/ # Scripts run hourly
To add a system-level cron job, either edit /etc/crontab or drop a script into one of the cron.d directories:
# /etc/cron.d/myapp
30 3 * * * root /opt/myapp/bin/cleanup.sh >> /var/log/myapp-cleanup.log 2>&1
Handling Output and Logging
By default, cron emails command output to the user. Redirect output to a file instead:
# Redirect stdout and stderr to a log file
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
# Discard all output silently
*/5 * * * * /usr/local/bin/check.sh > /dev/null 2>&1
# Append with timestamp
0 * * * * echo "$(date): ran check" >> /var/log/hourly-check.log
Environment Variables in Cron
Cron runs with a minimal environment. Your .bashrc is not sourced. Set variables at the top of your crontab:
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=admin@example.com
0 2 * * * /usr/local/bin/backup.sh
When running scripts that rely on custom paths or Python/Node environments, always use full absolute paths.
Special Shortcuts
@reboot /usr/local/bin/startup.sh # Run once at boot
@hourly /usr/local/bin/hourly.sh # Equivalent to: 0 * * * *
@daily /usr/local/bin/daily.sh # Equivalent to: 0 0 * * *
@weekly /usr/local/bin/weekly.sh # Equivalent to: 0 0 * * 0
@monthly /usr/local/bin/monthly.sh # Equivalent to: 0 0 1 * *
Debugging Cron Jobs
# Check cron daemon logs
grep CRON /var/log/syslog | tail -20 # Ubuntu/Debian
journalctl -u cron | tail -20 # systemd systems
# Test your script manually as the cron user
sudo -u www-data /usr/local/bin/myapp-cron.sh
# Check if the job ran at expected time
grep "backup.sh" /var/log/syslog
Anacron — For Systems That Are Not Always On
Cron requires the system to be running at the scheduled time. Anacron runs missed jobs after the system comes back up — useful for laptops and non-24/7 servers.
cat /etc/anacrontab
# Format: period delay job-identifier command
Summary
Cron is simple but powerful. Master the five-field syntax, always redirect output to logs, use full paths in your commands, and test scripts manually before scheduling them. For production systems, consider adding monitoring to alert you if a critical cron job stops running — a silent failure is worse than a noisy one.