This can also be done via an elevated command prompt using the sc
command. The syntax is:
sc config [service name] depend= <Dependencies(separated by / (forward slash))>
Note: There is a space after the equals sign, and there is not one before it.
Warning: depend=
parameter will overwrite existing dependencies list, not append. So for example, if ServiceA already depends on ServiceB and ServiceC, if you run depend= ServiceD
, ServiceA will now depend only on ServiceD. (Thanks Matt!)
Examples
Dependency on one other service:
sc config ServiceA depend= ServiceB
Above means that ServiceA will not start until ServiceB has started. If you stop ServiceB, ServiceA will stop automatically.
Dependency on multiple other services:
sc config ServiceA depend= ServiceB/ServiceC/ServiceD/"Service Name With Spaces"
Above means that ServiceA will not start until ServiceB, ServiceC, and ServiceD have all started. If you stop any of ServiceB, ServiceC, or ServiceD, ServiceA will stop automatically.
To remove all dependencies:
sc config ServiceA depend= /
To list current dependencies:
sc qc ServiceA
The poster wants to ensure the service is stopped before trying to restart it. You can use a loop on the output of "sc query" doing something like this:
:stop
sc stop myservice
rem cause a ~10 second sleep before checking the service state
ping 127.0.0.1 -n 10 -w 1000 > nul
sc query myservice | find /I "STATE" | find "STOPPED"
if errorlevel 1 goto :stop
goto :start
:start
net start | find /i "My Service">nul && goto :start
sc start myservice
Best Answer
The failure response is stored in the
REG_BINARY
valueFailureActions
underHKLM\SYSTEM\CurrentControlSet\services\<SERVICE_NAME>
. You'll probably have to set it the way you want it on one machine usingservices.msc
, grab the data from theFailureActions
value in the Registry, and then script the configuration withreg add
(orreg import
if using a .REG file).