Link to home
Start Free TrialLog in
Avatar of DADITWebGroup
DADITWebGroupFlag for United States of America

asked on

What is the best method of utilizing a batch file to kill Windows 2003 Server services in T-SQL???

I need to generate a batch file that will interactively disable a specific group of services within a SQL job.  These services have to be disabled one at a time and in sequence so that one service has been verified to be disabled before the next one can be disabled.  The batch file to kill services in general is pretty basic (I know), but what I would like to know is if anyone has experience building something a little more dynamic with some type of loop that verifies that the present service being disabled has in fact been positvely disabled before the batch moves to the next item.  All these services have to be disabled in order for me to be able to run the proper maintenance required for the databases.  Any ideas?
Avatar of Qlemo
Qlemo
Flag of Germany image

Sure. I would use something like the code below. The ping will wait for a 6-1 seconds between each check. If you really want to disable the service in addition to stopping it, add
sc config %%S start= disabled
before or after the sc stop.

@echo off
set svcs=Server Workstation Browser Nonsense
 
for %%S in (%svcs%) do (
  sc stop %%S
:wait
  ping -n 6 127.0.0.1 >nul
  sc query %%S | find "STATE" | find "STOPPED" >nul || goto wait
)
    

Open in new window

Avatar of DADITWebGroup

ASKER

This looks great, however I will give you an example of how I am trying to use it and the error I receive.  What am I dong wrong?  I tried using a local file path and this example represents the network path.  I am stopping the sql agent just for testing purposes.




Stop-srv.JPG
Avatar of Shaktur
Shaktur

In "set svcs=" you need to put the actual name of the service, putting in the network location will not work.
ASKER CERTIFIED SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial