Automation is the backbone of efficient system administration and web development. In Linux and Unix-like operating systems, the most reliable way to schedule repetitive tasks is by using Cron.
Whether you need to perform daily database backups, clear temporary logs every hour, or run a custom script at midnight, Cron is the tool that makes it happen in the background.
What is Cron?
Cron is a time-based job scheduler. It runs as a daemon (a background process) that constantly monitors configuration files to see if a command is scheduled to run at the current moment. Each scheduled task is known as a Cronjob.
Managing Your Cronjobs
You don’t need to manually edit system files to schedule tasks. Users can manage their own personal schedules using the crontab command.
- To edit/create your cronjobs: Run
crontab -ein your terminal. - To view your active jobs: Run
crontab -l. - Automatic Reload: You do not need to restart your computer or the cron service after making changes. Cron automatically detects updates to the crontab file.
Understanding the Syntax
A cronjob entry follows a specific five-star pattern representing different units of time:
* * * * * /path/to/command
| Field | Meaning | Allowed Values |
| 1st | Minute | 0 – 59 |
| 2nd | Hour | 0 – 23 |
| 3rd | Day of Month | 1 – 31 |
| 4th | Month | 1 – 12 |
| 5th | Day of Week | 0 – 7 (Sunday is 0 or 7) |
Easy Reference:
Minute Hour Day_of_Month Month Day_of_Week Command
Easy to remember format:
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
Using Operators for Complex Schedules
Operators allow you to group or skip time slots:
- Asterisk (
*): Represents “every.” An asterisk in the minute field means “every minute.” - Comma (
,): Specifies a list.1,5,10in the minute field runs at 1, 5, and 10 minutes past the hour. - Dash (
-): Defines a range.1-5in the day field runs from the 1st to the 5th of the month. - Slash (
/): Specifies intervals.*/15in the minute field means “every 15 minutes.”
Time-Saving Shortcuts
For common tasks, you can use readable aliases instead of the five-star syntax:
@reboot— Run once at system startup.- @yearly – Run once a year, “0 0 1 1 *”.
- @annually – (same as @yearly)
@hourly— Run once an hour (0 * * * *).@daily— Run once a day at midnight (0 0 * * *).@weekly— Run once a week (0 0 * * 0).@monthly— Run once a month (0 0 1 * *).- @midnight – (same as @daily)
Practical Examples
- Run a backup script every day at 3 AM:0 3 * * * /home/user/scripts/backup.sh
- Clear a cache folder every Sunday at midnight:0 0 * * 0 rm -rf /var/www/html/cache/*
- Run a custom command every 30 minutes:*/30 * * * * /usr/bin/python3 /home/user/script.py
Conclusion
Cron is a lightweight and powerful way to automate your workflow. By mastering the simple syntax and shortcuts, you can ensure your server stays healthy and your tasks are completed on time without manual intervention.




Comments