Skip to main content
Participant
February 25, 2022
Answered

CF Adminstrator scheduled task to run at the last day of the year using CRON TIME expression

  • February 25, 2022
  • 1 reply
  • 397 views

Cron expression to schedule the task which will run at 11:45 pm at the end of the year


    This topic has been closed for replies.
    Correct answer Charlie Arehart

     

     

    0 45 23 31 12 ?

     

     

    This is what you need for it to work in the CF sched task mechanism. You may have found instead resources online suggesting it could be just this:

     

     

    45 23 31 12 *

     

     

    ...such as at https://crontab.guru/#45_23_31_12_*. But if you use that for a CF task, you'd get:

     

     

    CronExpression '45 23 31 12 *' is invalid.

     

     

    The problem is in how the underlying Quartz open source library (which CF uses for scehduled tasks) expects its cron values to be specified.

     

    Traditional crontab format is:

     

     

    minute, hour, day of month, month, day of week

     

     

    The quartz cron pattern instead adds to that support for seconds (first field, required) and year (last field, optional):

     

     

    second, minute, hour, day of month, month, day of week, year

     

     

    Also, while the * for day of week (last value in my proposed expression) should work (according to the Quartz docs), I found that it did not, but the ? did. Technically, the ? should only be needed when some following field would be required but you wanted to imply "any" for the field with the "?". Logically, either should have worked. Oh well.

     

    FWIW, CF2021 still uses only quartz 2.2.1, so I linked to the docs 2.2.2, which is close enough:

     

    Also, here is a nifty online tool I found to help generate (or understand) a Quartz cron string.  (I was reminded of it it after posting my answer, and I want to offer it here to help future readers.) It, too, shows using that ? in the 6th field...though it then shows using also the * for the 7th.  As I noted above that's not needed, but I'll note here it does work (to switch the last "?" with "? *").

     

    Let us know if that gets you going. I tested it (using a date and time of today) and it did work for me, in CF2021.

    1 reply

    Charlie Arehart
    Community Expert
    Charlie ArehartCommunity ExpertCorrect answer
    Community Expert
    February 25, 2022

     

     

    0 45 23 31 12 ?

     

     

    This is what you need for it to work in the CF sched task mechanism. You may have found instead resources online suggesting it could be just this:

     

     

    45 23 31 12 *

     

     

    ...such as at https://crontab.guru/#45_23_31_12_*. But if you use that for a CF task, you'd get:

     

     

    CronExpression '45 23 31 12 *' is invalid.

     

     

    The problem is in how the underlying Quartz open source library (which CF uses for scehduled tasks) expects its cron values to be specified.

     

    Traditional crontab format is:

     

     

    minute, hour, day of month, month, day of week

     

     

    The quartz cron pattern instead adds to that support for seconds (first field, required) and year (last field, optional):

     

     

    second, minute, hour, day of month, month, day of week, year

     

     

    Also, while the * for day of week (last value in my proposed expression) should work (according to the Quartz docs), I found that it did not, but the ? did. Technically, the ? should only be needed when some following field would be required but you wanted to imply "any" for the field with the "?". Logically, either should have worked. Oh well.

     

    FWIW, CF2021 still uses only quartz 2.2.1, so I linked to the docs 2.2.2, which is close enough:

     

    Also, here is a nifty online tool I found to help generate (or understand) a Quartz cron string.  (I was reminded of it it after posting my answer, and I want to offer it here to help future readers.) It, too, shows using that ? in the 6th field...though it then shows using also the * for the 7th.  As I noted above that's not needed, but I'll note here it does work (to switch the last "?" with "? *").

     

    Let us know if that gets you going. I tested it (using a date and time of today) and it did work for me, in CF2021.

    /Charlie (troubleshooter, carehart. org)
    Participant
    February 25, 2022

    thanks. 

    Charlie Arehart
    Community Expert
    Community Expert
    February 25, 2022

    Glad to help. Could you please mark my first response as the "answer", to help others.

     

    BTW, I just tweaked it to add a link to an online generator of Quartz cron strings that you may find helpful.

     

    Finally, given your seeming preference for extreme brevity :-), I sincerely think you may find you'd REALLY enjoy the CFML slack channel, where most conversations are in mere sentences. Not my cup of tea, obviously. 🙂

    /Charlie (troubleshooter, carehart. org)