8

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.

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.

The following lines are in the noddy batch file I created:

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.

Any ideas?

Thanks,

Andez

Andez
  • 5,588
  • 20
  • 75
  • 116

4 Answers4

16

The "net stop" command has a parameter which is not commented. This parameter is /yes and will automatically stop all the dependent services too

So to stop a service with or without dependencies you just type

net stop spooler /yes
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43
1am
  • 161
  • 1
  • 2
  • aha....! that's reminding me of a robot saying,... "are you sure? (yes/no) - ok bos - error. "are you sure? (yes/no) - yes - done :D – gumuruh Jul 26 '20 at 03:41
2

You can check which dependencies a service has by running sc qc <service>

And in order to script that and retrieve the dependencies you can put it in a for-loop

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.

1

Niklas batch file doesn't work for me.

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.

What you actually want are the services that depend on the service being killed. You get those with the EnumDepend command to 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.

Pedro
  • 12,032
  • 4
  • 32
  • 45
Mark
  • 101
  • 1
  • 9
1

To anyone experiencing similar problems:

It's important to remember that the SC command is asynchronous. The following may help:

Halt batch file until service stop is complete?

Community
  • 1
  • 1
matpop
  • 1,969
  • 1
  • 19
  • 36