Cron is the standard Linux job scheduler, used to automate recurring tasks like backups, log rotation, certificate renewal checks, and cleanup scripts. This guide covers cron syntax, common scheduling patterns, and troubleshooting silent failures.
Editing Your Crontab
crontab -e
To edit root's crontab (for system-wide tasks):
sudo crontab -e
Cron Syntax
* * * * * command-to-run
^ ^ ^ ^ ^
| | | | +---- day of week (0-7, Sunday = 0 or 7)
| | | +------ month (1-12)
| | +-------- day of month (1-31)
| +---------- hour (0-23)
+------------ minute (0-59)
Common Examples
# Every day at 2:00 AM
0 2 * * * /usr/local/bin/backup.sh
# Every 15 minutes
*/15 * * * * /usr/local/bin/check.sh
# Every Sunday at 3:00 AM
0 3 * * 0 /usr/local/bin/weekly-report.sh
# First day of every month at midnight
0 0 1 * * /usr/local/bin/monthly-cleanup.sh
Viewing Existing Cron Jobs
crontab -l
System-Wide Cron Directories
For scripts that should always run at a fixed interval regardless of user, you can also drop executable scripts into:
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
Logging Cron Output (Critical for Debugging)
Cron jobs fail silently by default. Always redirect output to a log file:
0 2 * * * /usr/local/bin/backup.sh >> /var/log/backup.log 2>&1
Why Cron Jobs Often "Don't Work"
Cron runs with a minimal environment — no user shell profile, limited PATH. Always use absolute paths in scripts and commands:
# Wrong
0 2 * * * backup.sh
# Correct
0 2 * * * /usr/local/bin/backup.sh
Checking Cron's Own Logs
sudo journalctl -u cron
Common Errors
Job doesn't run at all — verify the cron service is running: sudo systemctl status cron.
Script works manually but not via cron — almost always a PATH or environment variable issue; use full paths for every command inside the script.
Permission denied — ensure the script is executable: chmod +x /usr/local/bin/backup.sh.
Best Practices
- Always redirect output to a log for debugging silent failures
- Use absolute paths for every command and file reference inside scripts
- Test scripts manually before scheduling them
- Avoid scheduling heavy jobs at the top of the hour when many other systems also run tasks
FAQ
How do I run a cron job every 5 minutes?
Use */5 * * * * command.
Can I schedule a job for a specific timezone?
Cron uses the system timezone by default; check/set it with timedatectl.
Related Articles
- How to Set Up Automated VPS Backups (rsync, cron & Off-Site Storage)
- How to Set the Correct Timezone and Enable NTP on a Linux VPS
- How to Manage Services with systemd and systemctl
