ITEM: AF4903L
Running crontab entry first Sunday of each month
Question:
Env: AIX 3.2.5
Desc: Customer wants to setup a cron job that will run on the first
Sunday of each month. The syntax he wants to use in the crontab
file is:
00 20 1-7 * 0 file_to_run
He is looking at the crontab man page and it reads:
If both the day of the month and day of the week fields are
specified as an element or list, but the day of the week is an
asterisk, both lists are matched.
Action:
The customer's example actually matches the following crontab rule:
If either the month of the year or day of the month field is
specified as an element or list and the day of the week field
is also specified as an element or list, then any day matching
either the day of the week field, or month of the year and day
of the month fields are matched.
The customer should be seeing his script run on the first through
seventh of every month as well as for every Sunday.
I tested this scenario and the crontab entry suggested by the
customer does indeed work this way. Here is the test case I used:
crontab:
00 8 1-7 * 0 /tmp/file_to_run
file_to_run:
echo "Running at `date`" >> /tmp/file_to_run.file
I then manipulated the date through the month of February and
this is the output from /tmp/file_to_run.file:
Running at Wed Feb 1 08:00:00 CST 1995
Running at Thu Feb 2 08:00:00 CST 1995
Running at Fri Feb 3 08:00:01 CST 1995
Running at Sat Feb 4 08:00:00 CST 1995
Running at Sun Feb 5 08:00:01 CST 1995
Running at Mon Feb 6 08:00:01 CST 1995
Running at Tue Feb 7 08:00:00 CST 1995
Running at Sun Feb 12 08:00:01 CST 1995
Running at Sun Feb 19 08:00:00 CST 1995
Running at Sun Feb 26 08:00:01 CST 1995
I then tried the following workaround. In this test case the
crontab entry is changed to run on every Sunday. Then the script
is modified to test the date and see if it is the first through
the seventh. This worked just as the customer would like. Here
is the test case:
crontab:
00 8 * * 0 /tmp/file_to_run2
file_to_run2:
if [ `date +%d` -lt 8 ]
then
echo "Running at `date`" >> /tmp/file_to_run.file2
else
echo "Not Running at `date`" >> /tmp/file_to_run.file2
fi
Again, I manipulated the date through the month of February
and got the following results in /tmp/file_to_run.file2:
Running at Sun Feb 5 08:00:01 CST 1995
Not Running at Sun Feb 12 08:00:00 CST 1995
Not Running at Sun Feb 19 08:00:00 CST 1995
Not Running at Sun Feb 26 08:00:00 CST 1995
Support Line: Running crontab entry first Sunday of each month ITEM: AF4903L
Dated: January 1995 Category: N/A
This HTML file was generated 99/06/24~13:30:28
Comments or suggestions?
Contact us