windows 停止/启动服务 - 处理 STOP_PENDING 和 START_PENDING

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

Stop/Start Service - Handling STOP_PENDING and START_PENDING

windowsbatch-file

提问by Jo?o

How do you make your stop/start service scripts gracefully handling the services that get stuck in STOP_PENDING or START_PENDING...

你如何让你的停止/启动服务脚本优雅地处理卡在 STOP_PENDING 或 START_PENDING 中的服务......

Related with SC STOP and START. Stop and Start a service via batch or cmd file?

与 SC STOP 和 START 相关。通过批处理或 cmd 文件停止和启动服务?

I have one aproach in mind, but I don't like it.

我想到了一种方法,但我不喜欢它。

采纳答案by Jo?o

This is the way i'm approaching the stop.

这就是我接近停靠点的方式。

  • Stop: sc %host% stop %service% || ECHO ERROR! && EXIT -1
  • Loop waiting for state to be STOPPED. Loop with a maximum number of iterations (e.g. 30). Testing if service is stopped (e.g. sc %host% query %service% | find /i "STOPPED"). Wait some seconds in each iteration (e.g. 10). Reaching the maximum iterations, try to kill the process using taskkill (e.g. work the output of sc queryex Alerter | find /i "PID").
  • 停止:sc %host% 停止 %service% || 回声错误!&& 退出 -1
  • 循环等待状态停止。以最大迭代次数(例如 30)循环。测试服务是否停止(例如 sc %host% query %service% | find /i "STOPPED")。在每次迭代中等待几秒钟(例如 10)。达到最大迭代次数,尝试使用 taskkill 终止进程(例如,处理 sc queryex Alerter | find /i "PID" 的输出)。

回答by Beetee

For me sometimes a 'Windows service' is hanging when I have the window services.mscopen. I can't kill the process, neither use net stopor sc stop.

对我来说,当我services.msc打开窗户时,有时会挂起“Windows 服务” 。我不能终止进程,也不使用net stopsc stop

But when I close the services.mscwindow, the service immediately stops.

但是当我关闭services.msc窗口时,服务立即停止。

回答by AlexandruC

First open service.msc, in the properties dialog for your targeted service go to the Recovery Tab*(run -> services.msc -> right-click Target-Service-> Properties -> Recovery tab*)

首先打开service.msc,在目标服务的属性对话框中转到恢复选项卡*(运行 -> services.msc -> 右键单击目标服务-> 属性 -> 恢复选项卡*)

Make sure the First\Second\Subsequentfailuresare all set to : Take No Action. enter image description here

确保First\Second\Subsequentfailures都设置为 : Take No Actionenter image description here

Get the PID of your target service using: sc queryex ServiceName:

使用以下命令获取目标服务的 PID:sc queryex ServiceName

Kill the process using taskkill /f /pid your_PID_number

使用taskkill /f /pid your_PID_number 终止进程