Systems Administration
What is Cron?
Section titled “What is Cron?”Cron is a job scheduler that runs commands automatically at specified times on Unix-like systems.
Basic Syntax
Section titled “Basic Syntax”* * * * * command│ │ │ │ ││ │ │ │ └─── 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
Section titled “Common Examples”Every minute
Section titled “Every minute”* * * * * echo "Hello" >> /tmp/hello.logEvery hour at minute 30
Section titled “Every hour at minute 30”30 * * * * /path/to/script.shEvery day at 2:30 AM
Section titled “Every day at 2:30 AM”30 2 * * * /usr/bin/backup.shEvery Monday at 9 AM
Section titled “Every Monday at 9 AM”0 9 * * 1 /home/user/weekly-report.pyEvery 5 minutes
Section titled “Every 5 minutes”*/5 * * * * /usr/local/bin/check-status.shHow to Use Cron
Section titled “How to Use Cron”Edit your crontab
Section titled “Edit your crontab”crontab -eList current cron jobs
Section titled “List current cron jobs”crontab -lRemove all cron jobs
Section titled “Remove all cron jobs”crontab -rImportant Tips
Section titled “Important Tips”- Use full paths for commands and files
- Test your scripts manually first
- Check logs:
grep CRON /var/log/syslog - For output, redirect to a file:
command >> /tmp/output.log 2>&1 - Remember cron runs with minimal environment variables
Quick Reference
Section titled “Quick Reference”| Time | Cron Expression |
|---|---|
| Every minute | * * * * * |
| Hourly | 0 * * * * |
| Daily at midnight | 0 0 * * * |
| Weekly (Sunday) | 0 0 * * 0 |
| Monthly | 0 0 1 * * |
| Yearly | 0 0 1 1 * |
Example: Weekly Backup
Section titled “Example: Weekly Backup”# Edit crontabcrontab -e
# Add this line for weekly backup at 6 AM on Monday0 6 * * 1 /home/<USERNAME>/.local/bin/dropbox_uploader.sh upload /zata/zippy/<USERNAME> zata_backupSee dropbox-uploader.sh section for more details about this backup tool.