通过批处理文件停止依赖于另一个服务的 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7984712/
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
Stop a Windows Service that is dependant on another service via Batch File
提问by Andez
All,
全部,
I'm trying to stop a Windows service that we have created that is dependant on another service. I just want to stop both of the service using a batch file, sc command for example, where the services are running on a remote machine.
我正在尝试停止我们创建的依赖于其他服务的 Windows 服务。我只想使用批处理文件停止这两个服务,例如 sc 命令,其中服务在远程机器上运行。
I have tried stopping the services in the order of dependancy (least dependant first), but it does not stop the service.
我试过按照依赖的顺序停止服务(最低依赖优先),但它并没有停止服务。
For example Service1 depends upon Service2 which is configured within the Service settings in the Services console. I am running the script on my Windows 7 PC and the server runs Windows Server 2003.
例如,Service1 依赖于在服务控制台的服务设置中配置的 Service2。我在 Windows 7 PC 上运行脚本,服务器运行 Windows Server 2003。
The following lines are in the noddy batch file I created:
以下几行在我创建的 noddy 批处理文件中:
sc \SERVER stop "Service1"
sc \SERVER stop "Service2"
The output in the Command Console is:
命令控制台中的输出是:
D:\Test>sc \SERVER stop "Service2"
[SC] ControlService FAILED 1051:
A stop control has been sent to a service that other running services are dependent on.
The service Service2 will not stop. Service1 stops fine.
服务 Service2 不会停止。Service1 停止正常。
Any ideas?
有任何想法吗?
Thanks,
谢谢,
Andez
安德斯
回答by 1am
The "net stop" command has a parameter which is not commented. This parameter is /yes and will automatically stop all the dependent services too
“net stop”命令有一个没有注释的参数。这个参数是/yes,也会自动停止所有依赖的服务
So to stop a service with or without dependencies you just type
因此,要停止有或没有依赖项的服务,您只需键入
net stop spooler /yes
回答by NiklasJ
You can check which dependencies a service has by running sc qc <service>
您可以通过运行来检查服务具有哪些依赖项 sc qc <service>
And in order to script that and retrieve the dependencies you can put it in a for-loop
为了编写脚本并检索依赖项,您可以将其放入 for 循环中
Example:
例子:
@echo off
setlocal enabledelayedexpansion
set service=winmgmt
set server=server
for /f "Tokens=2 Delims=:" %%i in ('sc \%server% qc %service% ^| find /i "DEPENDENCIES"') do (
set depservice=%%i
rem removes spaces
set depservice=!depservice: =!
sc \%server% stop "!depservice!"
rem extra: accumulate all dependencies to one variable
set alldeps=!alldeps!, !depservice!
rem remove first ", " in variable
set alldeps=!alldeps=~2!
)
sc \%server% stop "%service%" && echo Both %service% and !alldeps! were stopped || echo Something went wrong stopping %service%
exit /b
The above will only work if the service that you want to stop only has one dependency.
以上仅当您要停止的服务只有一个依赖项时才有效。
回答by Mark
Niklas batch file doesn't work for me.
Niklas 批处理文件对我不起作用。
It appears that on Windows Server 2008 R2, the qc command shows the services that this service depends on. They are not relevant at this point, you can stop the service without causing a ripple in their life.
似乎在 Windows Server 2008 R2 上,qc 命令显示了该服务所依赖的服务。他们此时不相关,您可以停止服务而不会在他们的生活中引起涟漪。
What you actually want are the services that depend on the service being killed. You get those with the EnumDepend command to sc.exe.
你真正想要的是依赖于被杀死的服务的服务。您可以使用 EnumDepend 命令获得 sc.exe。
Unfortunately, the output syntax is quite a bit different, so you'll need to preserve the logic shown above but replace the parsing.
不幸的是,输出语法有很大不同,因此您需要保留上面显示的逻辑但替换解析。
回答by matpop
To anyone experiencing similar problems:
对于遇到类似问题的任何人:
It's important to remember that the SC command is asynchronous. The following may help:
重要的是要记住SC 命令是异步的。以下可能有帮助: