cron
cron is a time-based job scheduler daemon that runs commands at fixed times or intervals defined in crontab files. It is the standard tool for backups, log rotation, and recurring maintenance on Linux.
Understanding cron
What Is cron and How Does It Schedule Jobs?
cron is a background daemon that wakes every minute, checks every crontab, and runs any command whose schedule matches the current time. It runs jobs whether or not anyone is logged in. Nightly backups, weekly log cleanup, and certificate renewal checks are typical uses.
How to Read a Crontab Schedule
A crontab line has five time fields followed by the command.
# minute hour day-of-month month day-of-week command 0 1 * * * /opt/scripts/backup.sh Fields: minute 0-59, hour 0-23, day of month 1-31, month 1-12, day of week 0-7 (0 and 7 are both Sunday) * every value */15 every 15 units1-5 a range (Mon-Fri) 1,15 specific valuesHow to Create and Manage Cron Jobs
crontab -e # edit your crontabcrontab -l # list it # Daily backup at 01:00, with output captured0 1 * * * /opt/scripts/backup.sh >> /var/log/backup.log 2>&1 # Health check every 5 minutes*/5 * * * * /opt/scripts/health-check.sh # Log cleanup every Sunday at 03:000 3 * * 0 find /var/log/app -name '*.log' -mtime +30 -delete # Weekdays at 09:000 9 * * 1-5 /opt/scripts/morning-check.sh # Shortcuts@reboot /opt/scripts/startup.sh@daily /opt/scripts/daily.sh # Verify the daemon (the service is cron on Debian/Ubuntu, crond on RHEL)systemctl status cronSystem-wide jobs live in /etc/crontab and /etc/cron.d/. Files in /etc/cron.d/ and /etc/crontab need an extra user column before the command.
Why Do cron Jobs Fail Silently?
cron runs jobs with a minimal environment: no login shell, a short PATH (often just /usr/bin:/bin), and none of your .bashrc settings. To see exactly what a job sees, temporarily add * * * * * env > /tmp/cron-env.txt.
Troubleshooting cron
| Symptom | Command | What to Check |
|---|---|---|
| Job never runs | systemctl status cron |
Daemon running |
| Works manually, fails in cron | Check PATH | Use absolute paths |
| No output or errors | Add >> job.log 2>&1 |
Redirect both streams |
| Runs twice | ls /etc/cron.d/ |
Duplicate entries in two places |
Common MistakeRelying on PATH. Commands in
/usr/local/binare not found under cron. Use/usr/local/bin/node, notnode, or setPATH=at the top of the crontab.
Security
crontab -rdeletes all your jobs with no confirmation. Usecrontab -i -rto be prompted, and keep a copy withcrontab -l > crontab.bak.
Remembercron uses the server's local time zone, so a job scheduled for 01:00 can shift or repeat around daylight saving changes. Many teams run servers in UTC for this reason.
Frequently Asked Questions
Why does a script work in my terminal but fail under cron?
cron starts jobs with a stripped environment, so a short PATH and missing variables from your shell profile are the usual cause. Use absolute paths, set variables inside the script, and redirect output to a log file to see the real error.
Where does cron send job output?
By default it tries to email the output to the job's owner, which usually goes nowhere on servers without a mail setup. Redirect with `>> /var/log/job.log 2>&1` so failures are recorded.
When should I use a systemd timer instead of cron?
Timers give you logging in the journal, dependency ordering, and catch-up of missed runs with `Persistent=true`. Cron is simpler for quick recurring commands; timers suit services that already run under systemd.