Automate Anything with Cron Jobs
Cron lets you automate repetitive tasks so you never have to remember to run them manually again.
What
Cron is the built-in Linux scheduler that runs commands on a schedule. The crontab file defines when and what to run using a five-field time syntax (minute, hour, day of month, month, day of week). Each user has their own crontab, and the system has one too.
Why It Matters
Manual repetitive tasks are error-prone and waste time. Cron ensures backups, log rotations, health checks, and cleanup scripts run reliably without human intervention. It's one of the oldest and most dependable automation tools in Unix.
Example
# Edit your crontab
crontab -e
# Add this line to run a backup script at 2 AM every day
0 2 * * * /home/user/backup.sh
# Run a cleanup script every Sunday at midnight
0 0 * * 0 /home/user/cleanup.sh
# List all your scheduled cron jobs
crontab -l
# Cron time syntax:
# ββββββββββ minute (0-59)
# β ββββββββ hour (0-23)
# β β ββββββ day of month (1-31)
# β β β ββββ month (1-12)
# β β β β ββ day of week (0-7, Sun=0 or 7)
# * * * * * commandCommon Mistake
Forgetting to use absolute paths in cron jobs. Cron runs with a minimal PATH environment, so commands like 'python' or 'node' may not be found. Always use full paths like /usr/bin/python3 or /usr/local/bin/node.
Quick Fix
Use absolute paths for all commands and scripts in your crontab. Run 'which python3' to find the full path, and add PATH=/usr/local/bin:/usr/bin:/bin at the top of your crontab for safety.
Key Takeaways
- 1Cron uses a five-field time syntax: minute, hour, day, month, weekday
- 2Use 'crontab -e' to edit and 'crontab -l' to list your jobs
- 3Always use absolute paths β cron has a minimal PATH
- 4Redirect output to a log file to debug failures: >> /var/log/myjob.log 2>&1
- 5Use https://crontab.guru to validate your cron expressions
Was this tip helpful?
Help us improve the DevOpsPath daily collection