Linux: cron jobs
Revision as of 12:33, 18 May 2017 by Rafahsolis (talk | contribs)
Linux cron tab format
MIN HOUR DOM MON DOW CMD
| Field | Description | Allowed Value |
|---|---|---|
| MIN | Minute | 0-59 |
| HOUR | Hour | 0-23 |
| DOM | Day of Month | 1-31 |
| MON | Month | 1-12 |
| DOW | Day of the week | 0-6 |
| CMD | Command | Any command to execute |
view/edit crontab
$ crontab -l → To view crontab $ crontab -e → Edit crontab $ crontab -u otherusername -e → Edit other user crontab
=== Schedule a job every 5 minutes
*/5 * * * * /home/ramesh/backup.sh
Schedule a job for a specific day
example: 10th June 08:30 AM.
30 08 10 06 * /home/ramesh/full-backup
- 30 – 30th Minute
- 08 – 08 AM
- 10 – 10th Day
- 06 – 6th Month (June)
- * – Every day of the week
Schedule a job twice a day
00 11,16 * * * /home/ramesh/bin/incremental-backup
- 00 – 0th Minute (Top of the hour)
- 11,16 – 11 AM and 4 PM
- * – Every day
- * – Every month
- * – Every day of the week
Schedule a job during working hours
00 09-18 * * * /home/ramesh/bin/check-db-status
- 00 – 0th Minute (Top of the hour)
- 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
- * – Every day
- * – Every month
- * – Every day of the week
Schedule a job during working hours excluding Saturday and Sunday
00 09-18 * * 1-5 /home/ramesh/bin/check-db-status
- 00 – 0th Minute (Top of the hour)
- 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
- * – Every day
- * – Every month
- 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)
Cron jobs special codes
| Keword | Equivalent |
|---|---|
| @yearly | 0 0 1 1 * |
| @daily | 0 0 * * * |
| @hourly | 0 * * * * |
| @reboot | Run at startup. |
@yearly /home/ramesh/red-hat/bin/annual-maintenance @monthly /home/ramesh/suse/bin/tape-backup @daily /home/ramesh/arch-linux/bin/cleanup-logs "day started" @reboot CMD
Adding paths to cron jobs
Add the following line to crontab to specify the location of the scriptss (/home/user/scripts)
This will allow to avoid having to type the full path to the script
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/user/scripts