Overview¶
Cron is a time-based job scheduler in Linux used to automate repetitive tasks such as:
-
Running scripts
-
System maintenance
-
Backups
-
Log rotation
It allows commands or scripts to run automatically at specified times or intervals.
Cron Job Format¶
A cron job consists of 6 fields:
Structure¶
* * * * * command
| | | | |
| | | | +---- Day of week (0–6) (Sunday=0)
| | | +-------- Month (1–12)
| | +------------ Day of month (1–31)
| +---------------- Hour (0–23)
+-------------------- Minute (0–59)
Field Explanation¶
| Field | Description |
|---|---|
| Minute | 0–59 |
| Hour | 0–23 |
| Day of Month | 1–31 |
| Month | 1–12 |
| Day of Week | 0–6 |
Special Characters¶
| Symbol | Meaning |
|---|---|
* |
Any value |
, |
Multiple values |
- |
Range |
/ |
Step values |
Examples¶
Run Every Minute¶
Run Every 5 Minutes¶
Run Every Hour¶
Run Daily at Midnight¶
Run at 2 AM Daily¶
Run Every Weekday at 5 PM¶
Run Every Sunday at 9:30 AM¶
Run First Day of Every Month at 8 AM¶
Special Strings¶
Instead of writing full cron expressions, you can use shortcuts:
| String | Equivalent |
|---|---|
@reboot |
Run at startup |
@hourly |
Every hour |
@daily |
Every day |
@weekly |
Every week |
@monthly |
Every month |
@yearly |
Every year |
Examples¶
Run Script at Startup¶
Run Every Hour¶
Run Daily¶
Managing Cron Jobs¶
Edit Cron Jobs¶
Explanation
-
Opens crontab editor
-
Add or modify scheduled tasks
List Cron Jobs¶
Remove All Cron Jobs¶
Warning
- Deletes all scheduled jobs permanently
Practical Examples¶
Backup Script Daily¶
Clean Logs Every Week¶
Run Script Every 15 Minutes¶
Output Handling¶
Redirect Output to File¶
Discard Output¶
Important Notes¶
-
Always use absolute paths in cron jobs
-
Cron runs with limited environment variables
-
Test scripts before scheduling
-
Monitor logs for failures
-
Be careful with overlapping jobs
Best Practices¶
-
Use comments in crontab
-
Schedule heavy jobs during low usage
-
Backup crontab entries
-
Use logging for debugging
-
Avoid running multiple instances of the same job
Summary Table¶
| Command | Purpose |
|---|---|
crontab -e |
Edit cron jobs |
crontab -l |
List cron jobs |
crontab -r |
Remove all jobs |
* * * * * |
Schedule format |
@daily |
Run daily |
@reboot |
Run at startup |
Conclusion¶
Cron is a powerful automation tool in Linux that helps:
-
Schedule repetitive tasks
-
Automate system maintenance
-
Improve efficiency
Understanding cron syntax and best practices allows you to build reliable and automated workflows in Linux environments.