Azure Timer Trigger: Cron expression for every hour on the hour

user1186050 picture user1186050 · Nov 22, 2018 · Viewed 9.6k times · Source

Whats the cron expression for every hour on the hour? I tried this '0 0 * ? * *' but it threw an error I have a Azure function timer trigger

This expression below runs every minute on the minute.

public static void Run([TimerTrigger("0 * * * * *")]TimerInfo myTimer, TraceWriter log)

Here is the error I received when I tried "0 0 * ? * * *" and "0 0 * ? * *"

[11/22/2018 12:45:29 AM] A ScriptHost error has occurred [11/22/2018 12:45:29 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'Currencies.Run'. Microsoft.Azure.WebJobs.Extensions: The schedule expression '0 0 * ? * * *' was not recognized as a valid cron expression or timespan string. [11/22/2018 12:45:29 AM] Error indexing method 'Currencies.Run' [11/22/2018 12:45:29 AM] Microsoft.Azure.WebJobs.Host: Error indexing method 'Currencies.Run'. Microsoft.Azure.WebJobs.Extensions: The schedule expression '0 0 * ? * * *' was not recognized as a valid cron expression or timespan string.

Answer

Mitch Wheat picture Mitch Wheat · Nov 22, 2018

Edit: Try using "0 0 * * * *", the doc emphasizes the use of six fields.

Azure Functions uses the NCronTab library to interpret CRON expressions. A CRON expression includes six fields:

{second} {minute} {hour} {day} {month} {day-of-week}


Azure supposedly uses the NCrontab library according to the doco, but testing suggests otherwise! so use "0 * * * *" for every hour on the hour.

Here's the LINQPad script to verify:

var s = CrontabSchedule.Parse("0 * * * *");
var start = new DateTime(2000, 1, 1);
var end = start.AddYears(1);
var occurrences = s.GetNextOccurrences(start, end);
occurrences.Dump();

[This needs the NCrontab nuget package pulled in]

Ref: https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-timer

"Azure Functions uses the NCronTab library to interpret CRON expressions"