12.3 Understanding Cron Syntax in the Job Scheduler

The cron triggers you can configure in the Orchestration Server Job Scheduler use a Quartz crontrigger class for deciding when to invoke job execution. This is based on the standard Quartz format that you can find further described on the KickJava website.

This section includes the following information:

12.3.1 Format

A cron expression is a string comprised of 6 or 7 fields separated by white space. Fields can contain any of the allowed values, along with various combinations of the allowed special characters for that field. The fields are explained in the following table:

Table 12-2 Fields in a Cron Expression

Field Name

Mandatory?

Allowed Values

Allowed special Characters

Seconds

Yes

0-59

, - * /

Minutes

Yes

0-59

, - * /

Hours

Yes

0-23

, - * /

Day of the Month

Yes

1-31

, - * ? / L W

Month

Yes

1-12 or JAN-DEC

, - * /

Day of the Week

Yes

1-7 OR SUN-SAT

, - * ? / L #

Year

No

EMPTY, 1970-2099

, - * /

Cron expressions can be as simple as this:

* * * * ? *

Or cron expressions can be more complex, like this:

0 0/5 14,18,3-39,52 ? JAN,MAR,SEP MON-FRI 2002-2011

12.3.2 Special Characters

Cron syntax incorporates logical operators, which are special characters that perform operations on the values provided in the cron fields.

Table 12-3 Special Characters in the Orchestration Server Cron Syntax

Operator

Purpose

Example

asterisk ( * )

Specifies all possible values for a field

An asterisk in the hour time field is equivalent to “every hour.”

question mark (?)

A question mark ( ? ) is allowed in the day-of-month and day-of-week fields. It is used to specify “no specific value,” which is useful when you need to specify something in one of these two fields, but not in the other.

If you want a trigger to fire on a particular day of the month (for example, the 10th), but you don't care what day of the week that is, enter 10 in the day-of-month field, and ? in the day-of-week field.

dash ( - )

Specifies a range of values

2-5, which is equivalent to 2,3,4,5

comma ( , )

Specifies a list of values

1,3,4,7,8

slash ( / )

Used to skip a given number of values

*/3 in the hour time field is equivalent to 0,3,6,9,12,15,18,21. The asterisk ( * ) specifies “every hour,” but the /3 means only the first, fourth, seventh.

You can use a number in front of the slash to set the initial value. For example, 2/3 means 2,5,8,11, and so on.

L (“last”)

The L character is allowed for the day-of-month and day-of-week fields.

Specifies either the last day of the month, or the last xxx day of the month.

The value L in the day-of-month field means “the last day of the month,” which is day 31 for January, or day 28 for February in non-leap years. If you use L in the day-of-week field by itself, it simply means 7 or SAT. But if you use it 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.”

HINT:When you use the L option, be careful not to specify lists or ranges of values. Doing so causes confusing results.

W (“weekday”)

The W character is allowed for the day-of-month field.

Specifies the weekday (Monday-Friday) nearest the given day.

If you 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 fires on Friday the 14th. If the 15th is a Sunday, the trigger fires on Monday the 16th. If the 15th is a Tuesday, it fires on Tuesday the 15th. However, if you specify 1W as the value for day-of-month, and the 1st is a Saturday, the trigger fires on Monday the 3rd, because it does 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.

HINT:You can combine the L and W characters for the day-of-month expression to yield LW, which translates to “last weekday of the month.”

pound sign ( # )

The pound sign ( # ) character is allowed for the day-of-week field. This character is used to specify “the nth” xxx day of the month.

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 specifies the first Monday of the month and 4#5 specifies the fifth Wednesday of the month. However, if you specify #5 and there are fewer than 5 of the given day-of-week in the month, 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.

You can specify days in two fields: month day and weekday. If both are specified in an entry, they are cumulative, meaning that both of the entries are executed.

12.3.3 Examples of Cron Syntax

The following table shows examples of full cron expressions and their respective meanings.

Table 12-4 Results of Altered Cron Syntax on Execution Times

Cron Expression Example

Description

0 0 12 * * ?

Fire at 12:00 p.m. (noon) every day

0 15 10 ? * *

Fire at 10:15 a.m. every day

0 15 10 * * ?

Fire at 10:15 a.m. every day

0 15 10 * * ? *

Fire at 10:15 a.m. every day

0 15 10 * * ? 2012

Fire at 10:15 a.m. every day during the year 2012

0 * 14 * * ?

Fire every minute starting at 2:00 p.m. and ending at 2:59.p.m., every day

0 0/5 14 * * ?

Fire every five minutes starting at 2:00 p.m. and ending at 2:55 p.m., every day

0 0/5 14,18 * * ?

Fire every five minutes starting at 2:00 p.m. and ending at 2:55 p.m., and fire every five minutes starting at 6:00 p.m. and ending at 6:55 p.m., every day

0 0-5 14 * * ?

Fire every minute starting at 2:00 p.m. and ending at 2:05.p.m., every day

0 10,44 14 ? 3 WED

Fire at 2:10 p.m. and at 2:44 p.m. every Wednesday in the month of March

0 15 10 ? * MON-FRI

Fire at 10:15 a.m. every Monday, Tuesday, Wednesday, Thursday and Friday

0 15 10 15 * ?

Fire at 10:15 a.m. on the 15th day of every month

0 15 10 15 * ?

Fire at 10:15 a.m. on the last day of every month

0 15 10 ? * 6L

Fire at 10:15 a.m. on the last Friday of every month

0 15 10 ? * 6L 2011-2014

Fire at 10:15 a.m. on every last Friday of every month during the years 2011, 20012, 2014, and 2014

0 15 10 ? * 6#3

Fire at 10:15 a.m. on the third Friday of every month

0 0 12 1/5 * ?

Fire at 12:00 p.m. (noon) every five days every month, starting on the first day of the month

0 11 11 11 11 ?

Fire every November 11th at 11:11 a.m.

12.3.4 Cron Scheduling Precautions

You should remember the following items when you use cron scheduling:

  • Always check the effect of adding the ? and * characters in the day-of-week and day-of-month fields to ensure that the expected behavior fires correctly.

  • Support for specifying both a day-of-week and a day-of-month value is not complete. You must currently use the ? character in one of these fields.

  • Be careful when setting fire times to occur between 12:00 a.m. and 1:00 a.m. Changing to or from daylight saving time can cause a skip or a repeat in the schedule firing, depending on whether the clock moves backward or forward.