Modern Cron

Power Manager supports a modern cron expression format.

cron is a scheduling tool first released in 1975 and included in many operating systems since.

cron’s strength lies in the power of it’s textual representation of time and date matching patterns. Complex ranges and sequencies of dates can be encoded in a short strings of letters and numbers. This power comes at the cost of complexity for the person writing the patterns.

A cron expression encodes a pattern that is matched against the current date and time. If the pattern matches, then the associated task is performed.

The default cron expression is a series of fives asterisks (*):

* * * * *

The five asterisk expression matches once a minute.

Expressions can become increasingly complex, and specific, by using lists, ranges, and steps:

*/10 9-17 * MAR,6,AUG-12 +MON-FRI

The expression above takes advantage of many of cron’s features. This expression matches on every weekday, on the tenth minute of every hour, between nine in the morning and five in the afternoon, but only in the months of March, June, August, October, November, and December.

This is a deliberately obtuse example as it both demonstrations the power and complexity of the format.

Looking in isolation at each field in this five field expression, we can decipher the following:

*/10
Every 10th minute of the hour. * is a wildcard and expands to the range 0-59, or more verbosely the list 0,1,2…,58,59. This list of possible minutes is then stepped through in increments of 10; leaving the list as 0,10,20,30,40,50.
9-17
Hours of the day between 9:00 (9am) and 17:00 (5pm).
*
Every day of the month.
MAR,6,AUG-12
Only in the months of March, June, and August through December. This field combines a range with a list. The list items can be written in any order and without abbreviations. So the following are all equivalent: 3,6,8-12, 3,6,8,9,10,11,12, 12,3,6,11,9,8,10, AUG-DEC,JUN,MAR.
+MON-FRI
Every weekday. That is every Monday, Tuesday, Wednesday, Thursday, or Friday. The plus symbol means the day of the week must be matched.

As the above example shows cron expressions can be impressively complex. Care needs to be taken not to over complicate the expression. Consider using other Power Manager triggers where possible, as these tend to be easier to understand.

Cron on macOS

In macOS, Apple deprecate cron in favour of launchd. Part of the decision to move away from BSD’s cron implementation was the computational cost. Traditional cron implementations need to check for matching patterns every minute, or even every second. This is reasonable for always-on servers but not for computers designed to be low power and able to sleep.

Power Manager’s implementation of cron does not require checking every minute or second. Matches are pre-calculated and enqueued for triggering by the Scheduler.

Cron Expression Format

The cron expression format is a series of five, six, or seven fields separated by spaces. Each field represents a pattern to match against a specific part of the current date and time.

The minimum five fields are:

The six field expression adds a seconds pattern.

The seven field expression adds seconds and year patterns to the minimum five fields.

Power Manager’s cron expression format supports:

Nicknames

Cron expressions can be a single nickname. These are shortcuts for well known and common expressions. Power Manager supports the following nicknames:

@daily
@midnight
Every day at midnight.
@weekly
Every Sunday at midnight.
@monthly
On the first day of the month at midnight.
@yearly
@annually
On January 1st at midnight.
@reboot
On start up.

Date Range

Cron expressions can represent dates within the years 1970 - 2199.

Abbreviations

The day of week and month fields can use abbreviations to improve legibility.

Day of Week

Days of the week can be represented numerically from 0-7 or with the abbreviations:

SUN
0 or 7, Sunday; both numeric values 0 and 7 represent Sunday.
MON
1, Monday
TUE
2, Tuesday
WED
3, Wednesday
THU
4, Thursday
FRI
5, Friday
SAT
6, Saturday

Months

Months can be represented numerically from 1–12 or with the abbreviations:

JAN
1, January
FEB
2, February
MAR
3, March
APR
4, April
MAY
5, May
JUN
6, June
JUL
7, July
AUG
8, August
SEP
9, September
OCT
10, October
NOV
11, November
DEC
12, December