如何停止Windows服务,检查它是否已停止,并使用Cmd Line重新启动它?

时间:2013-02-27 16:40:24

标签: command-line cmd command-prompt

我有一个相当简单的任务要实现,但在命令行脚本中是一个总的noob。我已经google了一下,发现了一些与Windows机器上的服务交互的基本命令,但没有检查服务是启动还是停止的方面。我希望有人可以帮助一个脚本停止3个服务,检查以确保所有3个服务都停止,并且一旦所有服务都停止了......再次启动它们。我需要确保在启动任何3个备份之前停止所有3个。我知道如何停止和启动服务,但剩下的就是我被困住的地方。

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

我想出了这个,但不要认为语法是正确的......

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

好吧我拿了上面的脚本并把它改成了一个while循环..它没有正确地循环吗?它检查了状态然后退出:(

  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

1 个答案:

答案 0 :(得分:0)

请尝试此脚本

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