为什么这个批处理脚本不执行?

时间:2017-03-03 14:29:41

标签: batch-file

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,而是我将其用作匿名的占位符。

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

1 个答案:

答案 0 :(得分:1)

   exit goto :stopSuccess

表示“退出cmd”,一旦检测到“STOPPED”,程序退出。

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