user1732364
user1732364

Reputation: 985

How to stop a windows service, check if its been stopped, and start it up again using Cmd Line?

I have a rather simple task to achieve but am a total noob at command line scripts. I have googled around and found some of the basic commands to interact with the services on a windows machine but nothing in aspects of checking whether a service is started or stopped. I'm hoping someone can help with a script that will stop 3 services, check to make sure all 3 are stopped, and once all are stopped..start them all back up again. I need to make sure all 3 are stopped before starting any of them back up. I know how to stop and start services but the rest is where i am stuck.

net Stop S1
net Stop S2
net Stop S3

//Check if all 3 are stopped

//If all 3 have been stopped successfully
net start s1
net start s2
net start s3

I have come up with this but dont think the syntax is correct...

net stop PriceIdxImport
net stop RawDataImportDB
net stop DBDailyTrxProcessService

set Pstate = sc query PriceIdxImport state
set Rstate = sc query RawDataImportDB state
set Dstate = sc query DBDailyTrxProcessService state

if Pstate == Inactive if Rstate == Inactive if Dstate == Inactive GOTO sub_StartServices

:sub_StartServices
net start PriceIdxImport
net start RawDataImportDB
net start DBDailyTrxProcessService

Alright i took the above script and altered it into a while loop..it doesnt do the loop correctly? It checked the status and then exits :(

  echo on
 net stop PriceIdxImport
 net stop RawDataImportDB
 net stop DBDailyTrxProcessService
 ping -n 5 127.0.0.1 > nul
 set Pstate = Sc query PriceIdxImport  |find /i "STOPPED"
 set Rstate = sc query RawDataImportDB |find /i "STOPPED"
 set Dstate = sc query DBDailyTrxProcessService |find /i "STOPPED"
 set Condition="false"

 while Condition="false";do if Pstate neq "" ( if Rstate  neq "" (if  Dstate neq ""          GOTO sub_StartServices));done
exit

:sub_StartServices
set Condition="true"
net start PriceIdxImport
net start RawDataImportDB
net start DBDailyTrxProcessService

Upvotes: 0

Views: 8357

Answers (1)

Travis G
Travis G

Reputation: 1602

Please try this script

echo on
:sub_StopServices    
net stop PriceIdxImport
net stop RawDataImportDB
net stop DBDailyTrxProcessService

set Pstate = Sc query PriceIdxImport  |find /i "STOPPED"
set Rstate = sc query RawDataImportDB |find /i "STOPPED"
set Dstate = sc query DBDailyTrxProcessService |find /i "STOPPED"

if Pstate neq "" ( if Rstate  neq "" (if  Dstate neq "" (GOTO sub_StartServices) else (GOTO sub_StopServices) ) else (GOTO sub_StopServices)) else (GOTO sub_StopServices)
exit
:sub_StartServices
net start PriceIdxImport
net start RawDataImportDB
net start DBDailyTrxProcessService

Upvotes: 1

Related Questions