The Linux crontab Command: My Automated Nightmares
I set up a cron job to run every minute. "Just */1 * * * *", I thought. Two days later, our server was crawling—5000+ jobs ran instead of 60. The disk filled up, and the system came to a crawl.
That's when I learned: cron jobs are powerful but must be precise.
First cron Syntax
Basic format
┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of month (1 - 31)
│ │ │ ┌───────────── month (1 - 12)
│ │ │ │ ┌───────────── day of week (0 - 6)
│ │ │ │ │
* * * * * command
Every minute
* * * * * /usr/local/bin/backup.shEvery hour
0 * * * * /usr/local/bin/backup.shEvery day at midnight
0 0 * * * /usr/local/bin/backup.shThe cron Mistakes That Almost Killed Me
1. Wrong asterisk
* * * * * # Every minute (correct)
*/1 * * * * # Every minute (also correct)
/1 * * * * # WRONG! Every 1st minute only2. No output handling
0 * * * * /script.sh # stdout/stderr go to email or nowhere!Need to redirect:
0 * * * * /script.sh >> /var/log/script.log 2>&13. Using relative paths
0 * * * * backup.sh # Can't find it!Use full paths:
0 * * * * /full/path/backup.sh4. Missing user
crontab -r # DELETES ALL JOBS!Use -i:
crontab -ri # Prompts before deletecrontab Commands That Work
Edit crontab
crontab -eOpens in editor.
List crontab
crontab -lShows current jobs.
Backup crontab
crontab -l > my-crontab.backupSave to file.
Remove crontab
crontab -rDelete all jobs.
Common cron Patterns
Daily at 2am
0 2 * * * /script.shWeekly (Sunday)
0 2 * * 0 /script.shMonthly
0 0 1 * * /script.shEvery 5 minutes
*/5 * * * * /script.shMultiple times
0 2,14 * * * /script.sh # 2am and 2pmThe crontab Command Builder
Building crontab commands—the crontab Command Builder:
- Visual schedule builder
- Cron expression builder
- Output redirect presets
Quick Reference
| Pattern | Meaning |
|---|---|
| * | Every |
| */n | Every n |
| n | At n |
| n,m | At n and m |
| n-m | Range |
| * * * * * | min hour dom mon dow |
Lessons Learned
-
Double-check expressions — test before deploying.
-
Always redirect output — cron drops stdout/stderr.
-
Use full paths — cron has minimal PATH.
-
Check system logs — /var/log/syslog shows cron activity.
-
Email notifications — MAILTO variable.
Conclusion: cron Is Automation
cron is powerful automation. Use it carefully.
The crontab Command Builder makes scheduling easy.
