|
The following description applies only to the Korn Shell
Script Implementation of crond named "ecrond_k93".
Do you want to use cron to schedule jobs to run, for example, on the
second Sunday of each quarter? Or on the first Saturday of each month?
Or some other combination of weekday occurrence and periodic schedule?
Typically, these types of schedules are handled by more advanced job
schedulers than cron, but the standard AIX cron daemon has the ability
to perform this type of scheduling.
Many business functions require processes to be executed based on
the day of the week and the occurrence of that weekday within a month,
quarter, or year. This document explains how to use cron to schedule
these functions without using scripts to perform date checking.
The following table displays seven calendars with the first day of
the month beginning on each of the seven weekdays. From these calendars
you can see the possible dates that may for each weekday, regardless of
what month or year it occurs. Highlighted in the following calendars are
the dates for the second Sunday in each month.
January 2006
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
|
April 2006
Sun Mon Tue Wed Thu Fri Sat
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
|
September 2006
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
|
June 2006
Sun Mon Tue Wed Thu Fri Sat
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30
|
February 2006
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28
|
August 2006
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
|
May 2006
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
|
From the above calendars, observe the second Sunday of any month can
have a date of the 8th, 9th, 10th, 11th, 12th, 13th, or 14th. Also
observe the second Sunday of any month can only occur on one of those
dates. In fact, this is true of any weekday, the second occurrence of
any weekday of any month may only occur on the 8th, 9th, 10th, 11th,
12th, 13th, or 14th of the month.
The first occurrence of any weekday of any month may only occur on
the 1st, 2nd, 3rd, 4th, 5th, 6th, or 7th of the month.
The third occurrence of any weekday of any month may only occur on
the 15th, 16th, 17th, 18th, 19th, 20th, or 21st of the month.
The fourth occurrence of any weekday of any month may only occur on
the 22nd, 23rd, 24th, 25th, 26th, 27th, or 28th of the month.
So how does this make a difference when scheduling jobs to
run through cron? Lets review the structure of a typical cron
record:
minute |
hour |
day of month |
month |
day of week |
command list |
15 |
2 |
1 |
* |
* |
/usr/local/scripts/somecommand |
The above cron record shows a command called
"/usr/local/scripts/somecommand " will be executed at 15
minutes after 2:00 AM on the first of the month, every month.
To schedule a job to run several times a day, multiple hours can be
specified, such as:
minute |
hour |
day of month |
month |
day of week |
command list |
15 |
2,6,10,14,20,22 |
* |
* |
* |
/usr/local/scripts/somecommand |
The above cron record shows a command called
"/usr/local/scripts/somecommand " will be executed every 4
hours beginning at 15 minutes after 2:00 AM, every day of every month.
So how can we use this cron record structure to allow us to
schedule a job to be executed every second Sunday of each
month?
From our previous discussion of dates and weekdays, we know that the
second Sunday of any month can only occur on the 8th, 9th, 10th, 11th,
12th, 13th, or 14th of the month. We also know there will be one and
only one second Sunday in each month. Using this information, we can
create the following cron record to represent a job schedule to be
executed on the second Sunday of each month:
minute |
hour |
day of month |
month |
day of week |
command list |
15 |
2 |
8,9,10,11,12,13,14 |
* |
0 |
/usr/local/scripts/somecommand |
The above cron record shows a command called
"/usr/local/scripts/somecommand " will be executed at 15
minutes after 2:00 AM on any Sunday where the date is the 8th, 9th,
10th, 11th, 12th, 13th, or 14th. The result of this schedule is the job
will only be executed on the second Sunday of each month.
Extending this concept to schedule jobs on a quarterly basis is
relatively easy. For example, to schedule a job to run on the 2nd
Sunday of each quarter, the cron record may appear as follows:
minute |
hour |
day of month |
month |
day of week |
command list |
15 |
2 |
8,9,10,11,12,13,14 |
3,6,9,12 |
0 |
/usr/local/scripts/somecommand |
The above cron record shows a command called
"/usr/local/scripts/somecommand " will be executed at 15
minutes after 2:00 AM on the second Sunday of March, June, September,
and December.
The important concepts to take from this document:
- Cron can be used to perform complex scheduling.
- Any first occurrence of any weekday occurs on the 1st, 2nd, 3rd,
4th, 5th, 6th, or 7th of any month.
- Any second occurrence of any weekday occurs on the 8th, 9th, 10th,
11th, 12th, 13th, or 14th of any month.
- Any third occurrence of any weekday occurs on the 15th, 16th, 17th,
18th, 19th, 20th, or 21st of any month.
- Any fourth occurrence of any weekday occurs on the 22nd, 23rd, 24th,
25th, 26th, 27th, or 28th of any month.
Happy Scheduling!!!
|
|