Skip to main content
Gainsight Inc.

Cron Expression for Advanced Scheduler in Rules Engine

Gainsight NXT

 

This article explains how to use Cron Expressions in scheduling Rules Engine. Gainsight recommends that you refer to the Schedule and Execute Rules article and then proceed with this article

Advanced Scheduler in Rules Engine uses Cron Expressions to schedule Rules. This enables you to schedule the rule runs more frequently, as compared to the existing (basic) scheduler. Rules can be run more than once per day. Some of the advanced use cases include running the rules as frequently as every two hours and every 1st and 15th of a month.

Note: The minimum time period between rule runs is two hours.

Cron_schedule_4.png

Format of Cron Expression

To provide flexibility in scheduling Rules, Gainsight allows you to input a  cron expression. This is a string comprising 6 or 7 fields that are separated by spaces and defines the details of a schedule. Each field represents a sub-expression and allows only specific values. These values combined with special characters define the schedule. Refer to the following table for allowed values and special characters in Cron Expression:

Field Order

Field Name

Mandatory

Allowed Values

Allowed Special Characters

1

Seconds

YES

0-59

, - * /

2

Minutes

YES

0-59

, - * /

3

Hours

YES

0-23

, - * /

4

Day of Month YES

1-31

, - * ? / L W

5

Month YES

1-12 or JAN-DEC

, - * /

6

Day of week YES

1-7 or SUN-SAT

, - * ? / L #

7

Year NO

Empty, 1970-2099

, - * /

Special Characters

The following is the list of special characters used in Cron Expressions:

  • * (“all values”) - used to select all values within a field. For example, “” in the minute field means *“every minute”.
  • ? (“no specific value”) - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, to fire a trigger on a particular day of the month (say, the 10th), but any day of the week, use  “10” in the day-of-month field, and “?” in the day-of-week field. See the Examples chapter for clarification.
  • - used to specify ranges. For example, “10-12” in the hour field means “the hours 10, 11 and 12”.
  • , used to specify additional values. For example, “MON,WED,FRI” in the day-of-week field means “the days Monday, Wednesday, and Friday”.
  • / used to specify increments. For example, “0/15” in the seconds field means “the seconds 0, 15, 30, and 45”. And “5/15” in the seconds field means “the seconds 5, 20, 35, and 50”. You can also specify ‘/’ after the ‘’ character - in this case ‘’ is equivalent to having ‘0’ before the ‘/’. ‘1/3’ in the day-of-month field means “fire every 3 days starting on the first day of the month”.
  • L (“last”) - has different meaning in each of the two fields in which it is allowed. For example, the value “L” in the day-of-month field means “the last day of the month” - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means “7” or “SAT”. But, if used in the day-of-week field after another value, it means “the last xxx day of the month” - for example “6L” means “the last friday of the month”. You can also specify an offset from the last day of the month, such as “L-3” which would mean the third-to-last day of the calendar month.
    Note: When using the ‘L’ option, it is important not to specify lists, or ranges of values, as you’ll get confusing/unexpected results.
  • W (“weekday”) - used to specify the weekday (Monday-Friday) nearest the given day. For example, to specify “15W” as the value for the day-of-month field, the meaning is: “the nearest weekday to the 15th of the month”. So, if the 15th is a Saturday, the trigger is fired on Friday the 14th. If the 15th is a Sunday, the trigger is fired on Monday the 16th. If the 15th is a Tuesday, then it is fired on Tuesday the 15th. However, if you specify “1W” as the value for day-of-month, and the 1st is a Saturday, the trigger is fired on Monday the 3rd, as it will not ‘jump’ over the boundary of a month’s days. The ‘W’ character can only be specified when the day-of-month is a single day, not a range or list of days.
    Note: The 'L' and 'W' characters can also be combined in the day-of-month field to yield 'LW', which translates to *"last weekday of the month"*.
  • # - used to specify “the nth” XXX day of the month. For example, the value of “6#3” in the day-of-week field means “the third Friday of the month” (day 6 = Friday and “#3” = the 3rd one in the month). Other examples: “2#1” = the first Monday of the month and “4#5” = the fifth Wednesday of the month. Note that if you specify “#5” and there is not 5 of the given day-of-week in the month, then no firing occurs that month.

Note: The legal characters and the names of months and days of the week are not case sensitive. MON is the same as mon.

Examples

Consider the following schedules and the respective Cron Expressions:

Rule run schedule

Cron Expression

Every 4 hours

0 0 */4 ? * *

Every 1st and 15th of a month

0 0 0 1,15 * ?

Once in every 90 days at 8:15 AM

0 15 8 1 1/3 ? *

Last day of every month at noon

0 0 12 L * ?

Every first Monday of the month at noon

0 0 12 ? 1/1 MON#1 *

Consider the Rule Run schedule for 'Once in every 90 days at 8:15 AM'. As the maximum no. of days in a month is 31, at order 4 -- 90 days cannot be represented. Instead at order 5 using 1/3 runs the rule once in three months and at order 4 rule can run on 1st of the month in every three months. This approximately runs the rule for 90 days. The next rule run schedules are: 

  1. Sunday, October 1, 2017 8:15 AM
  2. Monday, January 1, 2018 8:15 AM
  3. Sunday, April 1, 2018 8:15 AM
  4. Sunday, July 1, 2018 8:15 AM
  5. Monday, October 1, 2018 8:15 AM

Tools to generate Cron Expression 

Visit the following sites to generate a cron expression as required:

  • Was this article helpful?