Monday, January 23, 2012

Extended Cron

I wrote a Reminder/Notification program for Windows called Reminder 19 (you can check it out at http://reminder.relic19.net.)  I wanted to be able to cleanly define many different kinds of alert schedules and decided to use cron as my starting point.  I ended up extending the format because I wanted to have things like time delayed alerts and a year field.  This is the format I came up with... I think it's pretty expressive.  The only way I think it would be more expressive is if I added conditional alerts (for example, an alert should be shown on the 31st if the month has 31 days, otherwise it should be shown on the 28th).

Format 1: Advanced Schedule By Dates and Times
  +------------------ minute (0 - 59)
  |  +--------------- hour (0 - 23)
  |  |  +------------ day of month (0 - 31)
  |  |  |  +--------- month (1 - 12)
  |  |  |  |  +------ year (number between 1700 and 9999)
  |  |  |  |  |  +--- dayOfWeek (0 - 7, 10-17, 20-27, 30-37, 40-47)
  |  |  |  |  |  |  + optional command and parameters
  *  *  *  *  *  *  *

Each of the patterns from the first six fields may be either a single in range number, * (the asterisk character, which means the field matches all legal values), or a list of in range numbers separated by commas (such as 2,3). There may be no spaces following commas and fields may not end with a comma.  There is also an optional special seventh field.

The hour field (field 1) is specified using the 24-hour format.  0 is 12 AM, 4 is 4 AM, and 15 is 3 PM.

The month field (field 4) is defined as a number, where 1 is January and 11 is November.  Alerts will not be shown for non existent days, such as September 31.  Please note that an alert will not show on September 30 instead.

For the day of the week field (field 6), both 0 and 7 are considered Sunday.  The dayOfWeek field can be two digits, where the first digit represents its position in the month and the second digit is the day of the week.  For example, 31 would mean the third Monday of every month.  A position of 0 means every postion, so 03 means every Wednesday.  In deciding the next wake up time between, for example, the 1st and 31st, the soonest wake up date is chosen.

An alert is shown when the time/date specification fields all match the current time and date. There is one exception: if both "day of month" and "day of week" are restricted (not "*"), then either the "day of month" field (field 3) or the "day of week" field (field 6) must match the current day (even though the other of the two fields need not match the current day).

The command field (field 7) is completely optional.  If it is present, then an attempt will be made to execute the command.

Example 1:
00 16 1,2,31 2,3 2008 1,45
This alert specifies the following date and times:
February 1, 2008 at 4:00 PM
February 2, 2008 at 4:00 PM
February 4, 2008 at 4:00 PM (A Monday)
February 11, 2008 at 4:00 PM (A Monday)
February 18, 2008 at 4:00 PM (A Monday)
February 22, 2008 at 4:00 PM (The fourth Friday)
February 25, 2008 at 4:00 PM (A Monday)
March 1, 2008 at 4:00 PM
March 2, 2008 at 4:00 PM
March 3, 2008 at 4:00 PM (A Monday)
March 10, 2008 at 4:00 PM (A Monday)
March 17, 2008 at 4:00 PM (A Monday)
March 24, 2008 at 4:00 PM (A Monday)
March 28, 2008 at 4:00 PM (The fourth Friday)
March 31, 2008 at 4:00 PM (The 31st and a Monday)

Example 2:
00 4,16 * * * * "C:\Program Files\Internet Explorer\IEXPLORE.EXE" ? http://www.google.com
Every day at 4 AM and 4 PM, an instance of Internet Explorer will be launched that navigates to http://www.google.com.


Format 2: Advanced Schedules by Time Delays
  +------------------ the start minute (0 - 59)
  |  +--------------- the start hour (0 - 23)
  |  |  +------------ the start day of month (0 - 31)
  |  |  |  +--------- the start month (1 - 12)
  |  |  |  |  +------ the start year (number between 1700 and 9999)
  |  |  |  |  |  +--- increment in minutes (+[number of minutes to delay])
  |  |  |  |  |  |  + optional command and parameters
  * * * * *  *  *

No field in this format may be * (an asterisk character.)  Field 7 (the command field) is optional, but all other fields must be defined.

Each of the first 5 fields must be a single number in the specified ranges.  They will be used to define the start date.

The hour field is specified using the 24-hour format.  0 is 12 AM, 4 is 4 AM, and 15 is 3 PM.

The months are defined as number, where 1 is January and 11 is November.

For "increment in minutes" field (field 6), a non-negative number must be specified, prefixed by a '+' character.  The number represents the number of minutes to wait between alerts.  For example, +59 means that the alert will be triggered every 59 minutes after the start date.

The command field (field 7) is completely optional.  If it is present, then an attempt will be made to execute the command.

Example 1:
00 00 31 3 2008 +30
This alert will be shown every 30 minutes after March 31, 2008 at 12:00 AM.  So the first alert will go off at March 31, 2008 at 12:30 AM, the next alert will go off at March 31, 2008 at 1:00 AM, and so on.

Example 2:
00 00 31 3 2008 +60 "C:\Program Files\Internet Explorer\IEXPLORE.EXE" ? http://www.google.com
This alert will be shown every 60 minutes (one hour) after March 31, 2008 at 12:00 AM.  So the first alert will go off at March 31, 2008 at 1:00 AM, the next alert will go off at March 31, 2008 at 2:00 AM, and so on.  Whenever an alert goes off, http://www.google.com will be launched in Internet Explorer.

No comments: