For checking cron expressions in general, this will be highly dependent on the particular implementation you are using
rules
Typically, most adhere to the following format:
| Field name | Mandatory? | Allowed values | Special characters | | ------------- | ---------- | --------------- | ------------------ | | Seconds | No* | 0-59 | * / , - | | Minutes | Yes | 0-59 | * / , - | | Hours | Yes | 0-23 | * / , - | | Day of month | Yes | 1-31 | * / , - LW | | Month | Yes | 1-12 or JAN-DEC | * / , - | | Day of week | Yes | 0-6 or SUN-SAT | * / , - L # | | Year | No* | 1970β2099 | * / , - |
* where seconds and years are non-standard and sometimes not included
Some flavors allow for predefined time periods, for example:
| Entry | Equivalent to | | ---------- | ------------- | | @annually | 0 0 0 1 1 * * | | @yearly | 0 0 0 1 1 * * | | @monthly | 0 0 0 1 * * * | | @weekly | 0 0 0 * * 0 * | | @daily | 0 0 0 * * * * | | @hourly | 0 0 * * * * * | | @reboot | |
Intervals
Some variations allow you to use the @every <duration> syntax with the following time units:
| Unit | Definition | | ------ | ----------- | | ns | nanosecond | | us, Β΅s | microsecond | | ms | millisecond | | s | second | | m | minute | | h | hour |
Users
Regex Predefined Macros
To test predefined macros, you can use the following regular expression :
/@(annually|yearly|monthly|weekly|daily|hourly|reboot)/
Which will pass the following test cases:
@daily @hourly
@every regex
To check the duration of @every , you can use the following regular expression :
/@every (\d+(ns|us|Β΅s|ms|s|m|h))+/
Which will pass the following test cases:
@every 5s @every 20h30m
Individual Cron Terms
Checking cron terms in a regex is a bit more complicated since there are so many options.
Just by focusing on one term , all of the following are valid:
- Two or more numbers separated
, - Two numbers separated by
/ or - - 1-2 digit number
- One
*
To test a single term, we can use the following regular expression :
/(\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*/
where [TG415] just guarantees you have 1 or more numeric digits
Which will pass the following test cases:
1,2,3 1,2 1/2 1-2 1 *
Combining Cron Terms
To test the full expression, we can just make sure that we have {5,7} terms with the following regular expression :
/(((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7}/
If we wanted to distinguish between each term, we need to check the numbers in a certain range :
| Allowed values | Regex | | -------------- | ------------------------- | | 0-59 | [1-5]?[0-9] | | 0-23 | 2[0-3]|1[0-9]|[0-9] | | 1-31 | 3[01]|[12][0-9]|[1-9] | | 1-12 | 1[0-2]|[1-9] | | 0-6 | [0-6] | | 1970β2099 | 19[7-9][0-9]|20[0-9][0-9] |
If, however, we just want to make sure that something looks like an expression of a regular expression, without worrying about which term is days compared to hours, the expression remains much cleaner, so for simplicity I'll go
Putting it all together
Combining the above statements, we can get a relatively simple check that the term looks like a regular expression with an expression as follows :
/(@(annually|yearly|monthly|weekly|daily|hourly|reboot))|(@every (\d+(ns|us|Β΅s|ms|s|m|h))+)|((((\d+,)+\d+|(\d+(\/|-)\d+)|\d+|\*) ?){5,7})/
Additional Resources