Using date and cron to Run a Script on the Nth Day

Problem

You need to run a script on the Nth weekday of the month (e.g., the second Wednesday), and most crons will not allow that.

Solution

Use a bit of shell code in the command to be run. In your Linux Vixie Cron crontab adapt one of the following lines. If you are using another cron program, you may need to convert the day of the week names to numbers according to the schedule your cron uses (0–6 or 1–7) and use +%w (day of week as number) in place of +%a (locale’s abbreviated weekday name):

# Vixie Cron
# Min Hour DoM Mnth DoW Program
# 0-59 0-23 1-31 1-12 0-7

# Vixie Cron requires % to be escaped or you get an error!

# Run the first Wednesday @ 23:00
00 23 1-7 * Wed [ "$(date '+\%a')" == "Wed" ] && /path/to/command args to command

# Run the second Thursday @ 23:00
00 23 8-14 * Thu [ "$(date '+\%a')" == "Thu" ] && /path/to/command

# Run the third Friday @ 23:00
00 23 15-21 * Fri [ "$(date '+\%a')" == "Fri" ] && /path/to/command

# Run the fourth Saturday @ 23:00
00 23 22-27 * Sat [ "$(date '+\%a')" == "Sat" ] && /path/to/command

# Run the fifth Sunday @ 23:00
00 23 28-31 * Sun [ "$(date '+\%a')" == "Sun" ] && /path/to/command

Warning

Note that any given day of the week doesn’t always happen five times during one month, so be sure you really know what you are asking for if you schedule something for the fifth week of the month.

Warning

Note that Vixie Cron requires % to be escaped or you get an error like /bin/sh: 1: Syntax error: EOF in backquote substitution but other versions of cron may not, so check your man page.

Note

If cron seems like it’s not working, try restarting your MTA (e.g. sendmail). Some versions of cron on some systems (e.g Vixie Cron on Red Hat) are tied into the sendmail process. See https://bugzilla.redhat.com/show_bug.cgi?id=247228.

Discussion

Most versions of cron (including Linux’s Vixie Cron) do not allow you to schedule a job on the Nth day of the month. To get around that, we schedule the job to run during the range of days when the Nth day we need occurs, then check to see if it is the correct day on which to run. The “second Wednesday of the month” must occur somewhere in the range of the 8th to 14th day of the month. So we simply run every day and see if it’s Wednesday. If so, we execute our command.

Table 11-2 shows the ranges noted above.

Table 11-2. Day ranges for each week of a month

Week

Day range

First

1 to 7

Second

8 to 14

Third

15 to 21

Fourth

22 to 27

Fifth (see previous warning note)

28 to 31

We know this almost seems too simplistic; check a calendar if you don’t believe us:

$ cal 10 2006
   October 2006
 S  M  Tu   W  Th   F   S
 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

See Also

  • man 5 crontab

  • man cal

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.195.29