Skip to content

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:

* * * * * command_to_be_executed

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

* * * * * /path/to/script.sh

Run Every 5 Minutes

*/5 * * * * /path/to/script.sh

Run Every Hour

0 * * * * /path/to/script.sh

Run Daily at Midnight

0 0 * * * /path/to/script.sh

Run at 2 AM Daily

0 2 * * * /path/to/script.sh

Run Every Weekday at 5 PM

0 17 * * 1-5 /path/to/script.sh

Run Every Sunday at 9:30 AM

30 9 * * 0 /path/to/script.sh

Run First Day of Every Month at 8 AM

0 8 1 * * /path/to/script.sh

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

@reboot /path/to/script.sh

Run Every Hour

@hourly /path/to/script.sh

Run Daily

@daily /path/to/script.sh

Managing Cron Jobs

Edit Cron Jobs

crontab -e

Explanation

  • Opens crontab editor

  • Add or modify scheduled tasks


List Cron Jobs

crontab -l

Remove All Cron Jobs

crontab -r

Warning

  • Deletes all scheduled jobs permanently

Practical Examples

Backup Script Daily

30 2 * * * /home/user/backup.sh

Clean Logs Every Week

0 0 * * 0 /home/user/cleanup.sh

Run Script Every 15 Minutes

*/15 * * * * /home/user/script.sh

Output Handling

Redirect Output to File

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

Discard Output

* * * * * /script.sh > /dev/null 2>&1

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.