2017-03-03 73 views
0
sc stop myService > nul 

for /l %%A in (1,1,5) do (
    for /f "tokens=3 delims=: " %%H in ('sc query "myService" ^|findstr "STATE"') do (
    if /I "%%H" EQU "STOPPED" (
     exit goto :stopSuccess 
    ) else (
     echo Checking if the Source Agent service has stopped successfully.... 
     timeout 3 > nul 
    ) 
    ) 
) 

echo The service myService could not be stopped. 
goto :error 

:stopSuccess 
sc delete myService > nul 

当我尝试从cmd执行我的.bat脚本时,它不会执行任何操作并关闭cmd。显然,服务的名称实际上并不是myService,但我将它用作匿名的占位符。为什么不执行此批处理脚本?

我想要做的是做5间隔,间隔3秒,看看服务是否正常停止。然后我使用几个goto语句继续。

+1

你明确地有一个EXIT命令。 'GOTO:stopsuccess'不会执行,因为您在前面使用了EXIT。 – Squashman

+1

而且你也知道,你不能强制打破FOR/L命令。无论IF语句是TRUE,它都将执行5次。 – Squashman

回答

1
exit goto :stopSuccess 

意味着“退出cmd”,所以一旦检测到“STOPPED”,程序就会退出。

删除exit,它将按预期转到:stopsuccess

相关问题