Windows 任务调度程序可在几秒钟内执行任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7769635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows task scheduler to execute tasks in seconds
提问by Barry Jordan
I'm looking for an open source/free task scheduler for Windows 7 (development machine) that will allow me to schedule tasks (HTTP requests to a web service) to run every x seconds.
我正在寻找一个适用于 Windows 7(开发机器)的开源/免费任务调度程序,它允许我安排任务(对 Web 服务的 HTTP 请求)每 x 秒运行一次。
I've tried a couple of Cron clones and windows own Task Scheduler but neither seem to allow tasks to run at intervals less than 60 seconds. Am I missing something? I don't want to have to go and write any custom scripts either if possible.
我尝试了几个 Cron 克隆和 Windows 自己的任务计划程序,但似乎都不允许任务以小于 60 秒的间隔运行。我错过了什么吗?如果可能的话,我也不想去编写任何自定义脚本。
回答by Andre
It is possible to create multiple triggers for one scheduled task. If you create 59 identical triggers with an offset of 1 second to each other, and schedule the task itself to run every minute, you end up the scheduled task to run every second.
可以为一项计划任务创建多个触发器。如果您创建 59 个相同的触发器,彼此之间的偏移为 1 秒,并且将任务本身安排为每分钟运行一次,那么您最终将安排的任务每秒运行一次。
You could create those 59 triggers manually using the GUI. However, a much quicker way to create so many triggers is to create a task with one or two triggers, export it to a text file, duplicate the according lines, change the start offsets accordingly, and then re-import the file.
您可以使用 GUI 手动创建这 59 个触发器。但是,创建如此多触发器的更快方法是创建一个具有一两个触发器的任务,将其导出到文本文件,复制相应的行,相应地更改起始偏移量,然后重新导入文件。
回答by kralco626
I was actually able to achieve this.
我实际上能够做到这一点。
Update: Seems I over complicated it.
更新:似乎我把它复杂化了。
In the trigger, where it says "Repeat task every:"you can actually TYPEinto the drop-down "1 minute"(It wont let you type the time in seconds)
在触发器,它说:“重复任务间隔:”你其实可以TYPE到下拉“1分”(它不会让你键入秒的时间)
I did this on a Windows 7 machine.
我在 Windows 7 机器上做了这个。
Also, I clearly did not read the question well enough, as the asker seems to already have been able to get the time down to 1 minute. However, I'll leave this answer here, as it will explain for future readers exactly how to get the time down to one minute.
另外,我显然没有很好地阅读问题,因为提问者似乎已经能够将时间缩短到 1 分钟。但是,我将把这个答案留在这里,因为它将向未来的读者解释如何将时间缩短到一分钟。
It does seem as though you cannot get it to run at an interval of less than one minute.
似乎您无法让它以少于一分钟的间隔运行。
I set up a task with a trigger set to Daily to recur every 1 day. I check the "Repeat task every:" box. Setting it to 5 Minutes for a duration of 1 day
我设置了一个触发器设置为 Daily 的任务,每 1 天重复一次。我选中了“重复任务间隔:”框。将其设置为 5 分钟,持续 1 天
This makes the task go forever, every 5 minutes.
这使得任务永远进行,每 5 分钟一次。
I then exported the task. It exports to a .xml file.
然后我导出了任务。它导出到 .xml 文件。
Under Task > Triggers > CalendarTrigger > Repeition
there is the following tag: <Interval>PT5M</Interval>
I changed it from PT5M
to PT1M
. I re-imported the task.
根据Task > Triggers > CalendarTrigger > Repeition
有以下标签:<Interval>PT5M</Interval>
我的改变了它PT5M
到PT1M
。我重新导入了任务。
The task now runs every 1 minute.
该任务现在每 1 分钟运行一次。
I have not fully tested this, and I have not tried with less than one minute, but it might be possible by putting PT30S
or something for 30 seconds. I'll try it out and report back. Update: You cannot do this, you get an error when importing the task. It's not possible to set this time to less than 1 minute.
我还没有完全测试过这个,我还没有尝试过少于一分钟的时间,但可能可以通过放置PT30S
或其他东西 30 秒。我会试试看,然后回来报告。更新:您不能这样做,导入任务时会出错。不能将此时间设置为少于 1 分钟。
The whole trigger looks like this for me:
整个触发器对我来说是这样的:
<Triggers>
<CalendarTrigger>
<Repetition>
<Interval>PT1M</Interval>
<Duration>P1D</Duration>
<StopAtDurationEnd>false</StopAtDurationEnd>
</Repetition>
<StartBoundary>2013-11-07T17:04:51.6062297</StartBoundary>
<Enabled>true</Enabled>
<ScheduleByDay>
<DaysInterval>1</DaysInterval>
</ScheduleByDay>
</CalendarTrigger>
</Triggers>
回答by Barry Jordan
I've googled this to death, so as far as I can see the answer is, there are none. There are plenty of commercial solutions, but no open source/free programs.
我已经在谷歌上搜索到死,所以据我所知,答案是没有。有很多商业解决方案,但没有开源/免费程序。
I ended up writing a very simple periodic HTTP GET scheduler in java using quartz scheduler. It may be useful to other so posting a link to the source on guthub https://github.com/bjordan/simple_java_periodic_HTTP_scheduler
我最终使用quartz scheduler在java 中编写了一个非常简单的定期HTTP GET 调度程序。它可能对其他人有用,因此在guthub https://github.com/bjordan/simple_java_periodic_HTTP_scheduler上发布指向源的链接
回答by Mikael Chudinov
Short explanation: Main program starts a service process that will stay active in memory and will periodically activate a job – do something.
简短说明:主程序启动一个服务进程,该进程将在内存中保持活动状态,并会定期激活作业——做一些事情。
- Create a class that extends System.ServiceProcess.ServiceBase class
- Implement at least methods OnStart and OnStop
- Start and use Quartz.NET scheduler in OnStart to run tasks periodically
- 创建一个扩展 System.ServiceProcess.ServiceBase 类的类
- 至少实现方法 OnStart 和 OnStop
- 在 OnStart 中启动并使用 Quartz.NET 调度器定期运行任务
Here is my template C# solution for a Windows service and a Linux demon in .NET/Mono https://github.com/mchudinov/ServiceDemonAnd a short blogpost about it
这是我的模板 C# 解决方案,用于 .NET/Mono 中的 Windows 服务和 Linux 恶魔https://github.com/mchudinov/ServiceDemon以及关于它的简短博客文章
class Program
{
public static void Main(string[] args)
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new ServiceDemon.Service() };
ServiceBase.Run(ServicesToRun);
}
}
public class Service : ServiceBase
{
static IScheduler Scheduler { get; set; }
protected override void OnStart(string[] args)
{
StartScheduler();
StartMyJob();
}
protected override void OnStop()
{
Scheduler.Shutdown();
}
void StartScheduler()
{
ISchedulerFactory schedFact = new StdSchedulerFactory();
Scheduler = schedFact.GetScheduler();
Scheduler.Start();
}
void StartMyJob()
{
var seconds = Int16.Parse(ConfigurationManager.AppSettings["MyJobSeconds"]);
IJobDetail job = JobBuilder.Create<Jobs.MyJob>()
.WithIdentity("MyJob", "group1")
.UsingJobData("Param1", "Hello MyJob!")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("MyJobTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever())
.Build();
Scheduler.ScheduleJob(job, trigger);
}
}
public class MyJob : IJob
{
public void Execute(IJobExecutionContext context)
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
log.Info(dataMap["Param1"]);
}
}
class Program
{
public static void Main(string[] args)
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new ServiceDemon.Service() };
ServiceBase.Run(ServicesToRun);
}
}
public class Service : ServiceBase
{
static IScheduler Scheduler { get; set; }
protected override void OnStart(string[] args)
{
StartScheduler();
StartMyJob();
}
protected override void OnStop()
{
Scheduler.Shutdown();
}
void StartScheduler()
{
ISchedulerFactory schedFact = new StdSchedulerFactory();
Scheduler = schedFact.GetScheduler();
Scheduler.Start();
}
void StartMyJob()
{
var seconds = Int16.Parse(ConfigurationManager.AppSettings["MyJobSeconds"]);
IJobDetail job = JobBuilder.Create<Jobs.MyJob>()
.WithIdentity("MyJob", "group1")
.UsingJobData("Param1", "Hello MyJob!")
.Build();
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("MyJobTrigger", "group1")
.StartNow()
.WithSimpleSchedule(x => x.WithIntervalInSeconds(seconds).RepeatForever())
.Build();
Scheduler.ScheduleJob(job, trigger);
}
}
public class MyJob : IJob
{
public void Execute(IJobExecutionContext context)
{
JobDataMap dataMap = context.JobDetail.JobDataMap;
log.Info(dataMap["Param1"]);
}
}