如何使用cron运行年度定期工作 [英] how to run yearly recurring job using cron
问题描述
Hangfire使用ncrontab来执行重复作业。
Hangfire is using ncrontab to run recurring job.
contab下面的此操作是否每周仅在每周一的上午12点运行,仅仅2017年?
Does this below contab run every weekly Monday at 12 am for only 2017?
0 0 * * 1
如何确保它仅在2017年或每年运行?
How can I make sure it runs only for 2017 or every year?
推荐答案
Hangfire使用 NCrontab 库。如您所见,此库提供了一种快速测试其自身功能的方法。只需在Visual Studio中打开 C#Interactive
窗口并粘贴如下代码:
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}"));
另一种方法是使用在线crontab可视化工具。例如: cron .schlitt.info 。
Another way is to use online crontab visualizer. For example: cron.schlitt.info.
两个工具都给出相同的结果:您的cron表达式表示永远每周一运行。
Both tools give the same result: your cron expression means "run each Monday forever".
这篇关于如何使用cron运行年度定期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!