如何使用任务计划程序重新启动 Windows 服务

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

How to restart a windows service using Task Scheduler

windowsservicescheduled-tasks

提问by Kurt Van den Branden

The easiest way to do this is to create a batch file with:

最简单的方法是创建一个批处理文件:

NET stop <service name>
NET start <service name>

Once the batch file is created and tested, add it to Windows Task Scheduler and run it at a specific time interval. Problem here is, when the bat file is missing or corrupt, the service won't restart. So, are there other ways to restart a service at a specific time interval?

创建并测试批处理文件后,将其添加到 Windows 任务计划程序并以特定时间间隔运行。这里的问题是,当 bat 文件丢失或损坏时,服务不会重新启动。那么,是否有其他方法可以在特定时间间隔重新启动服务?

回答by Kurt Van den Branden

Instead of using a bat file, you can simply create a Scheduled Task. Most of the time you define just one action. In this case, create two actions with the NETcommand. The first one to stop the service, the second one to start the service. Give them a STOPand STARTargument, followed by the service name.

您可以简单地创建一个计划任务,而不是使用 bat 文件。大多数情况下,您只定义一个操作。在这种情况下,使用NET命令创建两个操作。第一个停止服务,第二个启动服务。给他们一个STOPandSTART参数,后跟服务名称。

In this example we restart the Printer Spooler service.

在本例中,我们重新启动了打印机后台处理程序服务。

NET STOP "Print Spooler" 
NET START "Print Spooler"

enter image description here

enter image description here

enter image description here

enter image description here

Note: unfortunately NET RESTART <service name>does not exist.

注意:不幸的NET RESTART <service name>是不存在。