Using crontab on Linux
Dec 23, 2012
The use of crontab in Linux is almost vital, however many users know nothing of how it works, or what it is used for. Crontab is basically a scheduler which is used to run scripts at intervals. Possibilities of usage are
- log rotation
- backups
- time synchronization
Crontab can he extremely handy, if you know how to use it ;-) Each line consisting of 6 fields separated by spaces.
Fields:
00,15,30,45 00-23/1 * * mon,tue,wed,thu,fri /root/backup_script
- minute of the hour, 00 to 59
- hour of the day, 00 to 23 (military time)
- day of the month, 1 to 31
- month of the year, 1 to 12
- day of the week, sun, mon, tue,….
- actual command to execute
- an asterisk that matches all possible values,
- a single integer that matches that exact value,
- a list of integers separated by commas (no spaces) used to match any one of the values
- two integers separated by a dash (a range) used to match any value within the range.
Manual Edit
To edit your own crontab:
crontab -e
Use special string to save time
Instead of the first five fields, you can use any one of eight special strings. It will not just save your time but it will improve readability.
Special string | Meaning |
---|---|
@reboot | Run once, at startup. |
@yearly | Run once a year, “0 0 1 1 *” |
@annually | (same as @yearly) |
@monthly | Run once a month, “0 0 1 * *” |
@weekly | Run once a week, “0 0 * * 0” |
@daily | Run once a day, “0 0 * * *” |
@midnight | (same as @daily) |
@hourly | Run once an hour, “0 * * * *” |
Run ntpdate every hour:
@hourly /path/to/ntpdate