.net 如何安排 Windows 服务的启动和停止?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18428213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:25:41  来源:igfitidea点击:

How to schedule the start and stop of a Windows service?

.netwindows-servicescron

提问by Giacomo Tesio

I've a .NET windows service that should start at 7:00 and stop at 23:00 each day, running continuously in background.

我有一个 .NET Windows 服务,它应该在每天 7:00 开始并在 23:00 停止,在后台连续运行。

While I can code the service so that it sleep between 23 and 7, I would prefer a system configuration (something like cronin unix).

虽然我可以编码服务使其在 23 到 7 之间休眠,但我更喜欢系统配置(类似于unix 中的cron)。

How can I do this on Windows 7?

如何在 Windows 7 上执行此操作?

Note that, if system boot up after 7:00, the service should start immediatly.

请注意,如果系统在 7:00 后启动,则该服务应立即启动。

回答by Moein Nourmohammadi

1- Create a batch file such as starter.bat and type NET START "SERVICE NAME"

1- 创建一个批处理文件,例如 starter.bat 并键入 NET START "SERVICE NAME"

2- Create a Task in Task Scheduler for 7:00 a.m that run batch file every day and remember to check Run task as soon as possible after a scheduled start is missedin Settings tab so it will start even if system boot up after 7 a.m.

2- 每天早上 7:00 在任务计划程序中创建一个任务,每天运行批处理文件,并记得在设置选项卡中错过计划启动后尽快检查运行任务,这样即使系统在早上 7 点之后启动它也会启动

Repeat those steps for stoper.bat include NET STOP "SERVICE NAME" for 23:00 p.m

对 stoper.bat 重复这些步骤,包括 NET STOP "SERVICE NAME" for 23:00 pm

回答by Rahul Tripathi

You could use Windows task schedulerfor this task or a schedule task.

您可以为此任务或计划任务使用Windows 任务计划程序

Also Windows AT command is very similar to Cron in Unix

此外,Windows AT 命令与 Unix 中的 Cron 非常相似

"The AT command schedules commands and programs to run on a computer at a specified time and date. The Schedule service must be running to use the AT command."

“AT 命令调度命令和程序在指定的时间和日期在计算机上运行。调度服务必须正在运行才能使用 AT 命令。”

回答by S.Spieker

Or you could write another service, let's call it guard which runs always und starts the other service depending on a config file for example.

或者您可以编写另一个服务,让我们称其为守卫,它始终运行并根据配置文件启动其他服务。

回答by Tony Hopkinson

Seeing as you need another service to manage the scheduling of the services. Write a service, that hosts service like thingies. The host deals with starting and stopping, even restarting in the event of a crash. You can even get clever and get it to look up with "services" to load an run, and applet to see what's going on and tweak the schedule , register and unregister services with the host.

认为您需要另一个服务来管理服务的调度。编写一个服务,它托管服务之类的东西。主机处理启动和停止,甚至在发生崩溃时重新启动。您甚至可以聪明地使用“服务”来查找以加载运行,并使用小程序查看正在发生的事情并调整时间表,向主机注册和取消注册服务。

An approach anyway.

反正一种方法。

回答by Minerbob

AT command gets deprecated at some point. this works server 2012 windows 10.

AT 命令在某些时候被弃用。这适用于服务器 2012 Windows 10。

schtasks /create /tn "StartWrapper" /tr "NET START 'Name of Service'" /sc daily /st 23:05 /RL HIGHEST

schtasks /create /tn "StartWrapper" /tr "NET START '服务名称'" /sc daily /st 23:05 /RL HIGHEST

You have to play with the quotes to escape the command correctly.

您必须使用引号才能正确转义命令。