Scheduling with Custom Cron Patterns#
Using custom cron patterns in your scheduling allows you more flexibility and customizable runs that are more specific than you can make with general scheduling.
In this demo, translate the cron pattern language and get on your way to better scheduling.
Breaking Down Cron Patterns#
In order to schedule with custom cron patterns, you must first understand what the combination of (wild cards) and numbers mean in the pattern.
There are 5 pieces to a cron pattern:
The minute is anything from 1-59. Hour is anything from 0-23. Day of Month allows between 1-31. Month accepts 1-12 or names (first 3 letters of month name). Day of Week is Sunday through Saturday 0-7 (0 or 7 represents Sunday).
You can also use the first three letters in Day of Week. The * represents first-last, or all.
Number | Time | Range | Output |
---|---|---|---|
1 | Minute | 1-59 | \ |
2 | Hour | 0-23 | \ |
3 | Day of month | 1-31 | \ |
4 | Month | 1-12 | Jan-Dec (first 3 letters) |
5 | Day of week | 0-7 | Sun-Sat (first 3 letters) |
Choosing A Schedule#
Now that you have an understanding of how a cron pattern is broken down, you need to decide on a schedule.
For example, if you want to run a job Monday through Friday every week at 4:05 PM every month:
The 5 represents the 5th minute of the hour, the 16 represents 4 PM. The ** calls all days of month and all months. The 1-5 calls Monday through Friday.
You can also write it like this:
Now we can get more advanced. Say you want to run a job at 10 AM and 6 PM, but only on the 1st and 15th of each month:
The 10 and 18 represents the two hours of the day that you want to run the job. The 1 and 15 represents the days of the month. The ** tells the job to run every month on any day. You can use the - or, to separate values in your cron pattern as we have in the past 2 examples.
Practicing Cron Patterns#
Now that you have learned the the basics of cron patterns, let's practice!
-
Navigate to where you have the Flight Delays application installed in Spectrum. Navigate to the "Resources" folder within the app and right-click the "Airports" import job > "Open"".
-
Click "Next" until you come to the "Schedule" tab and select "On a schedule" under 'Loading' and you see the "Cron Pattern" field box.
Exercises#
Now it's time to put your skills to work! Schedule the job to run correctly in the following exercises.
Exercise One#
You would like to run your job at the top of the hour from 9 AM to 6 PM every day.
Answer
0 9-18 * * *
Exercise Two#
You would like to run your job at 15 after the hour at 11 AM and 11 PM Monday through Friday.
Answer
15 11,23 * * Mon-Fri
or
15 11,23 * * 1-5
Exercise Three#
You would like to run your job at 9 AM on the 5 and 25th of November, December, and January.
Answer
0 9 5,25 Nov-Jan *
or
0 9 5,25 11-1 *