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 range0-59, or more verbosely the list0,1,2…,58,59. This list of possible minutes is then stepped through in increments of10; leaving the list as0,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:
- minutes
- hours
- days of month
- months
- days of week
The six field expression adds a seconds pattern.
- Six fields: seconds, minutes, hours, days of month, months, days of week
The seven field expression adds seconds and year patterns to the minimum five fields.
- Seven fields: seconds, minutes, hours, days of month, months, days of week, years
Power Manager’s cron expression format supports:
- five, six, and seven field patterns;
- wildcards (
*), with?as an alias in the day of month and day of week fields; - lists, i.e.
1,5,10,3,MON,WED,FRI - numeric ranges, i.e.
1-4,0-50; - range steps, i.e.
*/2,5-40/5,JAN-OCT/3; - nicknames, i.e.
@daily; - day of week required modifier (
+), i.e.+MON.
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:
SUN0or7, Sunday; both numeric values0and7represent Sunday.MON1, MondayTUE2, TuesdayWED3, WednesdayTHU4, ThursdayFRI5, FridaySAT6, Saturday
Months
Months can be represented numerically from 1–12 or with the abbreviations:
JAN1, JanuaryFEB2, FebruaryMAR3, MarchAPR4, AprilMAY5, MayJUN6, JuneJUL7, JulyAUG8, AugustSEP9, SeptemberOCT10, OctoberNOV11, NovemberDEC12, December