how to run yearly recurring job using cron

2019-08-17 14:30发布

问题:

Hangfire is using ncrontab to run recurring job.

Does this below contab run every weekly Monday at 12 am for only 2017?

0 0 * * 1

How can I make sure it runs only for 2017 or every year?

回答1:

Hangfire uses NCrontab library. As you could see, this lib provides a way to rapidly test its own functionality. Just open C# Interactive window in Visual Studio and paste a code like below:

#r "C:\Users\YOUR_USER\.nuget\packages\ncrontab\3.3.0\lib\netstandard1.0\NCrontab.dll"
using NCrontab;
var s = CrontabSchedule.Parse("0 0 * * 1");
var start = new DateTime(2000, 1, 1);
var end = start.AddYears(12);
var occurrences = s.GetNextOccurrences(start, end);
Console.WriteLine(string.Join(Environment.NewLine, 
    from t in occurrences
    select $"{t:ddd, dd MMM yyyy HH:mm}"));

Another way is to use online crontab visualizer. For example: cron.schlitt.info.

Both tools give the same result: your cron expression means "run each Monday forever".