这一部分是原创的
0 30-50/2 * * * ? 每小时30-50分钟每两分钟触发一次
0 20,25,30-50/2 * * * ? 每小时,20分一次,25分一次,再30-50分钟每两分钟触发一次
0 20,25,30-50 * * * ? 每小时,20分一次,25分一次,再30-50分钟每一分钟触发一次
0 20,25,30-50/1 * * * ? 每小时,20分一次,25分一次,再30-50分钟每一分钟触发一次
0 0 2,5,18-22/2 1,2,10-20/2 * ? 1号,2号,10-20号/每两天 的 2点,5点,18-22点/每2小时触发次(这个理论上是对的,但是实际上没有测试过。) 算下来是:
1号2点,1号5点,1号18点,1号20点,1号22点;
2号2点,2号5点,2号18点,2号20点,2号22点; 10号2点,10号5点,10号18点,10号20点,10号22点; 12号2点,12号5点,12号18点,12号20点,12号22点; 14号2点,14号5点,14号18点,14号20点,14号22点; 16号2点,16号5点,16号18点,16号20点,16号22点; 18号2点,18号5点,18号18点,18号20点,18号22点; 20号2点,20号5点,20号18点,20号20点,20号22点;不知道对不对!!
范例代码,代码可运行,代码是改的范例
using System;using System.Collections.Generic;using System.Diagnostics;using System.Threading.Tasks;namespace PowerCmdCall{ using Quartz; using Quartz.Impl; using System.Threading; using static System.Console; class Program { static void Main(string[] args) { if(args!=null && args[0] != null){ //RunBat(args[0]); BaseRunBatScript s = new BaseRunBatScript(); s.RunBat(args[0]); } // 创建调度实例,并运行 CronTriggerExample c = new CronTriggerExample(); c.Run(); ReadKey(); } // 这个是顺带的 执行bat并读取输出到界面上 private static void RunBat(string batPath) { using (Process pro = new Process()) { FileInfo file = new FileInfo(batPath); pro.StartInfo.WorkingDirectory = file.Directory.FullName; pro.StartInfo.FileName = batPath; pro.StartInfo.UseShellExecute = false; pro.StartInfo.RedirectStandardInput = true; pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = false; pro.Start(); //pro.BeginOutputReadLine(); //var result = pro.StandardOutput.ReadToEnd(); Console.WriteLine("----"); while (!pro.StandardOutput.EndOfStream) { var currLine = pro.StandardOutput.ReadLine(); Console.WriteLine(currLine); } Console.WriteLine("===="); //var result = pro.StandardOutput.ReadToEnd(); //Console.WriteLine(result); pro.WaitForExit(); pro.Close(); } } } // 对象化的基本运行脚本 public class BaseRunBatScript { public BaseRunBatScript() { CurrentRunLog = new StringBuilder(); CurrentErrorLog = new StringBuilder(); } public StringBuilder CurrentRunLog { get; set; } public StringBuilder CurrentErrorLog { get; set; } public virtual void Log(string info) { this.CurrentRunLog.Append(info); WriteLine(info); } public virtual void InitRun() { } public virtual void BeforRun() { } public virtual void AfterRun() { } public virtual void Running() { } public virtual void EndRun() { WriteLine(CurrentRunLog); } ////// /// /// public void RunBat(string batPath) { this.InitRun(); using (Process pro = new Process()) { FileInfo file = new FileInfo(batPath); pro.StartInfo.WorkingDirectory = file.Directory.FullName; pro.StartInfo.FileName = batPath; pro.StartInfo.UseShellExecute = false; pro.StartInfo.RedirectStandardInput = true; pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = false; this.BeforRun(); pro.Start(); //pro.BeginOutputReadLine(); //var result = pro.StandardOutput.ReadToEnd(); Log("----"); while (!pro.StandardOutput.EndOfStream) { var currLine = pro.StandardOutput.ReadLine(); this.Running(); this.Log(currLine); } Log("===="); //var result = pro.StandardOutput.ReadToEnd(); pro.WaitForExit(); this.AfterRun(); pro.Close(); } this.EndRun(); } } // 这个是 范例接口 public interface IExample { string Name { get; } void Run(); } // 基础工作 public class BaseJob { public string JobName { get; set; } public void Log(string info) { WriteLine(info); } public string Now() { return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); } } // 工作实例0 public class SimpleJob0 : BaseJob, IJob { ////// Called by the public virtual void Execute(IJobExecutionContext context) { // This job simply prints out its job name and the // date and time that it is running // job this.JobName = "SimpleJob0"; this.Log($"{this.JobName} == {this.Now()}"); } } // 工作实例1 public class SimpleJob1 : BaseJob, IJob { ///when a /// fires that is associated with /// the . /// /// Called by the public virtual void Execute(IJobExecutionContext context) { // This job simply prints out its job name and the // date and time that it is running // job this.JobName = "SimpleJob1"; this.Log($"{this.JobName} == {this.Now()}"); } } // 工作实例2 public class SimpleJob2 : BaseJob, IJob { ///when a /// fires that is associated with /// the . /// /// Called by the public virtual void Execute(IJobExecutionContext context) { // This job simply prints out its job name and the // date and time that it is running // job this.JobName = "SimpleJob2"; this.Log($"{this.JobName} == {this.Now()}"); } } // 工作实例3 public class SimpleJob3 : BaseJob, IJob { ///when a /// fires that is associated with /// the . /// /// Called by the public virtual void Execute(IJobExecutionContext context) { // This job simply prints out its job name and the // date and time that it is running // job this.JobName = "SimpleJob3"; this.Log($"{this.JobName} == {this.Now()}"); } } // 触发器实例 public class CronTriggerExample : IExample { public string Name { get { return "TEST CronTriggerExample"; } } public virtual void Run() { // First we must get a reference to a scheduler // 先创建一个 调度工厂 ISchedulerFactory sf = new StdSchedulerFactory(); // 通过工厂 再创建调度器 IScheduler sched = sf.GetScheduler(); // jobs can be scheduled before sched.start() has been called // job 1 will run every 20 seconds // 创建一个工作,指定工作的实例,必须实现接口ijob接口 IJobDetail job = JobBuilder.Createwhen a /// fires that is associated with /// the . /// () .WithIdentity("job1", "group1") .Build(); // 定义触发器,扳机。这里每天凌晨2点运行。具体的配置看后面的说明 ICronTrigger trigger = (ICronTrigger) TriggerBuilder.Create() .WithIdentity("trigger1", "group1") .WithCronSchedule("0 0 2 * * ?") .Build(); // 调度.....恩....其实我还没理解这里(感觉上是送入到调取系统中) DateTimeOffset ft = sched.ScheduleJob(job, trigger); // 第二个job定义 job = JobBuilder.Create () .WithIdentity("job2", "group1") .Build(); // 第二个触发器 trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger2", "group1") .WithCronSchedule("0 5 20 * * ?") .Build(); ft = sched.ScheduleJob(job, trigger); // ===============3 job = JobBuilder.Create () .WithIdentity("job3", "group1") .Build(); // 3 trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger3", "group1") .WithCronSchedule("0 45 8 * * ?") .Build(); ft = sched.ScheduleJob(job, trigger); // ===============4 job = JobBuilder.Create () .WithIdentity("job4", "group1") .Build(); // 4 trigger = (ICronTrigger)TriggerBuilder.Create() .WithIdentity("trigger4", "group1") .WithCronSchedule("0 0 0/1 * * ?") .Build(); ft = sched.ScheduleJob(job, trigger); // All of the jobs have been added to the scheduler, but none of the // jobs // will run until the scheduler has been started // 这里就运行 调度。里面包含4个任务,4个触发器(那么一个触发器多个任务呢?) sched.Start(); // 下面这里纯粹就是让电脑等着调度器运行,等6天 try { // wait five minutes to show jobs Thread.Sleep(6 * 24 * 60 * 60 * 1000); // executing... } catch (ThreadInterruptedException) { } // 关闭调度 sched.Shutdown(true); // 这里就是从前面的调度器中提取元数据。具体是些什么不知道呢! SchedulerMetaData metaData = sched.GetMetaData(); } }}
以下是 转载别人的
CronTrigger配置格式:
格式: [秒] [分] [小时] [日] [月] [周] [年]序号 说明 是否必填 允许填写的值 允许的通配符
1 秒 是 0-59 , - * / 2 分 是 0-59 , - * / 3 小时 是 0-23 , - * / 4 日 是 1-31 , - * ? / L W 5 月 是 1-12 or JAN-DEC , - * / 6 周 是 1-7 or SUN-SAT , - * ? / L # 7 年 否 empty 或 1970-2099 , - * / 通配符说明:* 表示所有值. 例如:在分的字段上设置 "*",表示每一分钟都会触发。? 表示不指定值。使用的场景为不需要关心当前设置这个字段的值。例如:要在每月的10号触发一个操作,但不关心是周几,所以需要周位置的那个字段设置为"?" 具体设置为 0 0 0 10 * ?- 表示区间。例如 在小时上设置 "10-12",表示 10,11,12点都会触发。, 表示指定多个值,例如在周字段上设置 "MON,WED,FRI" 表示周一,周三和周五触发/ 用于递增触发。如在秒上面设置"5/15" 表示从5秒开始,每增15秒触发(5,20,35,50)。 在月字段上设置'1/3'所示每月1号开始,每隔三天触发一次。L 表示最后的意思。在日字段设置上,表示当月的最后一天(依据当前月份,如果是二月还会依据是否是润年[leap]), 在周字段上表示星期六,相当于"7"或"SAT"。如果在"L"前加上数字,则表示该数据的最后一个。例如在周字段上设置"6L"这样的格式,则表示“本月最后一个星期五"W 表示离指定日期的最近那个工作日(周一至周五). 例如在日字段上设置"15W",表示离每月15号最近的那个工作日触发。如果15号正好是周六,则找最近的周五(14号)触发, 如果15号是周未,则找最近的下周一(16号)触发.如果15号正好在工作日(周一至周五),则就在该天触发。如果指定格式为 "1W",它则表示每月1号往后最近的工作日触发。如果1号正是周六,则将在3号下周一触发。(注,"W"前只能设置具体的数字,不允许区间"-").小提示 | 'L'和 'W'可以一组合使用。如果在日字段上设置"LW",则表示在本月的最后一个工作日触发(一般指发工资) |
# 序号(表示每月的第几个周几),例如在周字段上设置"6#3"表示在每月的第三个周六.注意如果指定"#5",正好第五周没有周六,则不会触发该配置(用在母亲节和父亲节再合适不过了)
小提示 | 周字段的设置,若使用英文字母是不区分大小写的 MON 与mon相同. |
0 0 12 * * ? | 每天12点触发 |
0 15 10 ? * * | 每天10点15分触发 |
0 15 10 * * ? | 每天10点15分触发 |
0 15 10 * * ? * | 每天10点15分触发 |
0 15 10 * * ? 2005 | 2005年每天10点15分触发 |
0 * 14 * * ? | 每天下午的 2点到2点59分每分触发 |
0 0/5 14 * * ? | 每天下午的 2点到2点59分(整点开始,每隔5分触发) |
0 0/5 14,18 * * ? | 每天下午的 2点到2点59分(整点开始,每隔5分触发) 每天下午的 18点到18点59分(整点开始,每隔5分触发) |
0 0-5 14 * * ? | 每天下午的 2点到2点05分每分触发 |
0 10,44 14 ? 3 WED | 3月分每周三下午的 2点10分和2点44分触发 |
0 15 10 ? * MON-FRI | 从周一到周五每天上午的10点15分触发 |
0 15 10 15 * ? | 每月15号上午10点15分触发 |
0 15 10 L * ? | 每月最后一天的10点15分触发 |
0 15 10 ? * 6L | 每月最后一周的星期五的10点15分触发 |
0 15 10 ? * 6L 2002-2005 | 从2002年到2005年每月最后一周的星期五的10点15分触发 |
0 15 10 ? * 6#3 | 每月的第三周的星期五开始触发 |
0 0 12 1/5 * ? | 每月的第一个中午开始每隔5天触发一次 |
0 11 11 11 11 ? | 每年的11月11号 11点11分触发(光棍节) |