The Linux crontab Command: My Automated Nightmares

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.sh

Every hour

0 * * * * /usr/local/bin/backup.sh

Every day at midnight

0 0 * * * /usr/local/bin/backup.sh

The cron Mistakes That Almost Killed Me

1. Wrong asterisk

* * * * *  # Every minute (correct)
*/1 * * * * # Every minute (also correct)
/1 * * * *  # WRONG! Every 1st minute only

2. No output handling

0 * * * * /script.sh  # stdout/stderr go to email or nowhere!

Need to redirect:

0 * * * * /script.sh >> /var/log/script.log 2>&1

3. Using relative paths

0 * * * * backup.sh  # Can't find it!

Use full paths:

0 * * * * /full/path/backup.sh

4. Missing user

crontab -r  # DELETES ALL JOBS!

Use -i:

crontab -ri  # Prompts before delete

crontab Commands That Work

Edit crontab

crontab -e

Opens in editor.

List crontab

crontab -l

Shows current jobs.

Backup crontab

crontab -l > my-crontab.backup

Save to file.

Remove crontab

crontab -r

Delete all jobs.


Common cron Patterns

Daily at 2am

0 2 * * * /script.sh

Weekly (Sunday)

0 2 * * 0 /script.sh

Monthly

0 0 1 * * /script.sh

Every 5 minutes

*/5 * * * * /script.sh

Multiple times

0 2,14 * * * /script.sh  # 2am and 2pm

The 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

  1. Double-check expressions — test before deploying.

  2. Always redirect output — cron drops stdout/stderr.

  3. Use full paths — cron has minimal PATH.

  4. Check system logs — /var/log/syslog shows cron activity.

  5. Email notifications — MAILTO variable.


Conclusion: cron Is Automation

cron is powerful automation. Use it carefully.

The crontab Command Builder makes scheduling easy.


Further Reading