Sub topic 2
Scheduling with cron: syntax and crontab files
This chapter assumes you can use the Linux command line, edit files, read basic shell scripts, and understand users, permissions, and exit codes.
What cron does
Cron is a scheduler that starts commands at specified times. A cron daemon runs continuously in the background and checks schedule files called crontabs. When a schedule matches the current time, cron starts the command.
A cron entry has five time fields followed by the command:
| Field | Allowed values | Meaning |
|---|---|---|
| Minute | 0–59 | Minute within the hour |
| Hour | 0–23 | Hour of the day |
| Day of month | 1–31 | Calendar day |
| Month | 1–12 | Month of the year |
| Day of week | 0–7 | Sunday is 0 or 7; Saturday is 6 |
For example:
30 2 * * * /usr/local/bin/backup.shThis starts the script every day at 02:30. An asterisk means “every allowed value.”
Writing cron expressions
Cron supports several useful patterns:
*/15means every 15 units, such as every 15 minutes.1-5means a range, such as Monday through Friday.1,15means a list, such as the first and fifteenth day.0in the hour field means midnight.7in the day-of-week field also means Sunday on common cron implementations.
Examples:
*/15 * * * * /usr/local/bin/sync.sh
0 6 * * 1-5 /usr/local/bin/report.sh
0 0 1 * * /usr/local/bin/monthly-cleanup.shThe first entry runs every 15 minutes. The second runs at 06:00 on weekdays. The third runs at midnight on the first day of each month.
When both the day-of-month and day-of-week fields contain restricted values, traditional cron implementations run the command when either field matches. This can surprise you. Test such expressions before using them for important work.
Editing a user's crontab
Each user can have a personal crontab. Edit your own with:
crontab -eList it with:
crontab -lThe editor saves the file for the cron daemon. A personal crontab does not include a username field because cron already knows which user owns it.
Root can edit another user's crontab:
sudo crontab -u backup -eThe system crontab, usually /etc/crontab, and files in /etc/cron.d/ use an extra username field:
30 2 * * * backup /usr/local/bin/backup.shDo not copy this line unchanged into a personal crontab. The backup field would be interpreted as part of the command.
Understanding a schedule visually
Practical rules
- Use absolute paths for commands and scripts, such as
/usr/bin/flock. - Make scripts executable with
chmod 750 script. - Put comments above entries to explain their purpose.
- Keep the command short by calling a maintained script.
- Remember that cron's environment is smaller than your interactive shell's environment. Set important variables and paths explicitly.
- Redirect output deliberately so errors do not disappear.
[email protected]
PATH=/usr/local/bin:/usr/bin:/bin
# Run the synchronization script every 15 minutes
*/15 * * * * /usr/local/bin/sync.sh >> /var/log/sync.log 2>&1Exercise
Write a cron entry that runs /usr/local/bin/report.sh at 06:30 every weekday.
Show answer
30 6 * * 1-5 /usr/local/bin/report.sh