Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop a Windows Service that is dependant on another service via Batch File

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

like image 758
Andez Avatar asked Nov 02 '11 17:11

Andez


People also ask

How do I stop a service using sc command?

Open Start. Search for Command Prompt, right-click the top result, and select the Run as administrator option. Type the following command to disable a service and press Enter: sc config "SERVICE-NAME" start=disabled In the command, replace "SERVICE-NAME" for the name of the service that you want to disable.

How do I stop a Windows service remotely?

Method 1: Using Command SC SC \computername STOP servicename. SC \computername START servicename.

How do I set dependencies in Windows services?

Open the Service Control Manager by typing in the command window “services. msc”. Browse for the service you have modified, right click and select “Properties“. Select the “Dependency” tab.


2 Answers

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
like image 126
1am Avatar answered Nov 13 '22 23:11

1am


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.

like image 35
Niklas J. MacDowall Avatar answered Nov 14 '22 00:11

Niklas J. MacDowall