Understanding cron
What Is cron in Simple Terms
cron is the Linux alarm clock for automated tasks. You tell it what to run and when, and it runs that command at exactly that time, every time, whether you are logged in or not. Backup at 1 AM every night: cron. Certificate renewal every 90 days: cron. Log cleanup every Sunday: cron.
How It Works
◈ DIAGRAM
+------------------------------------------+| crond daemon (runs continuously) || Wakes up every minute || Checks all crontab files |+------------------------------------------+ | match found | v+------------------------------------------+| Fork a child process || Set minimal environment || PATH=/usr/bin:/bin only || Execute the command as specified user |+------------------------------------------+ | v+------------------------------------------+| Command output: emailed to user || (unless redirected with >> logfile 2>&1) |+------------------------------------------+Crontab time field syntax:
◈ DIAGRAM
+---------- minute (0-59)| +-------- hour (0-23)| | +------ day of month (1-31)| | | +---- month (1-12)| | | | +-- day of week (0=Sun, 6=Sat)| | | | |* * * * * command to execute Special values:* every value*/15 every 15 units (*/15 in minute = every 15 min)1-5 range (1-5 in weekday = Mon through Fri)1,15 specific values (1st and 15th of month)Practical Commands
Bash
## Edit personal crontabcrontab -e ## List current crontabcrontab -l ## Common crontab examples:## Run backup daily at 1 AM0 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 3 AM0 3 * * 0 find /var/log/app -name '*.log' -mtime +30 -delete ## First day of month at midnight0 0 1 * * /opt/scripts/monthly-report.sh ## Weekdays at 9 AM0 9 * * 1-5 /opt/scripts/morning-check.sh ## System-wide cron jobsls /etc/cron.d/ ## arbitrary schedulesls /etc/cron.daily/ ## runs dailyls /etc/cron.hourly/ ## runs hourly ## Special shortcuts@reboot /opt/scripts/startup.sh ## run once at boot@daily /opt/scripts/daily.sh ## equivalent to 0 0 * * *@hourly /opt/scripts/hourly.sh ## equivalent to 0 * * * * ## Debug PATH issues## Add temporarily to see cron environment:* * * * * env > /tmp/cron-env.txt## Then: cat /tmp/cron-env.txt | grep PATH## PATH=/usr/bin:/bin <- much less than your interactive PATH ## Verify cron is runningsystemctl status cron ## Debian/Ubuntusystemctl status crond ## RHELTroubleshooting
| Symptom | Command | What to Check |
|---|---|---|
| Job not running | systemctl status cron |
cron daemon running? |
| Job runs manually but not in cron | Check PATH in script | Use absolute paths |
| No output or errors | Add >> /tmp/job.log 2>&1 |
Redirect output to file |
| Job runs twice | Check /etc/cron.d/ too | Duplicate entries in different files |
COMMON MISTAKE / WARNING**Common Mistake:** Using relative paths or commands not in cron's PATH. cron runs with a minimal PATH (`/usr/bin:/bin`). Commands in `/usr/local/bin`, `/snap/bin`, or other directories will not be found. Always use absolute paths like `/usr/local/bin/node` instead of just `node`.
REMEMBER THIS**Remember:** `crontab -r` removes ALL cron jobs with no confirmation and no undo. It is one typo away from `crontab -e`. If you accidentally run it, immediately check if you have a backup and restore with `crontab backup-file`. Consider aliasing `crontab` to `crontab -i` in your shell config to always prompt before removing.