1.1 Cron
1.1 Cron
2
Linux Cron utility is an effective way to schedule a routine background job at a
specific time and/or day on an on-going basis.
3
1. Scheduling a Job For a Specific Time
The basic usage of cron is to execute a job in a specific time as shown below. This will execute
the Full backup shell script (full-backup) on 10th June 08:30 AM.
Please note that the time field uses 24 hours format. So, for 8 AM use 8, and for 8 PM use 20.
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
4
2. Schedule a Job For More Than One Instance (e.g. Twice a Day)
This example executes the specified incremental backup shell script (incremental-backup) at
11:00 and 16:00 on every day. The comma separated value in a field specifies that the
command needs to be executed in all the mentioned time.
00 11,16 * * * /home/ramesh/bin/incremental-backup
5
3. Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)
If you wanted a job to be scheduled for every hour with in a specific range of time then use the following.
Cron Job everyday during working hours
This example checks the status of the database everyday (including weekends) during the working hours
9 a.m – 6 p.m
00 09-18 * * * /home/ramesh/bin/check-db-status
6
4. Cron Job every weekday during working hours
This example checks the status of the database every weekday (i.e excluding Sat and Sun)
during the working hours 9 a.m – 6 p.m.
7
5. How to View Crontab Entries?
[email protected]$ crontab -l
[email protected]# crontab -l
8
6. Schedule a Job for Every Minute Using Cron.
Ideally you may not have a requirement to schedule a job every minute. But understanding
this example will will help you understand the other examples mentioned below in this article.
* * * * * CMD
The * means all the possible unit — i.e every minute of every hour through out the year. More
than using this * directly, you will find it very useful in the following cases.
9
7. Schedule a Background Cron Job For Every 10 Minutes.
Use the following, if you want to check the disk space every 10 minutes.
*/10 * * * * /home/ramesh/check-disk-space
10
END of this Course Module.
Thanks
11