Eric Zhang
Eric Zhang

Reputation: 57

Why doesn't this batch script execute?

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

When I try to exectue my .bat script from cmd, it doesn't do anything and cmd closes. Obviously, the name of the service is not actually myService, but I'm using it as a placeholder for anonymity.

What I want to do is do 5 checks spaced 3 seconds apart to see if the service stopped properly. Then I use a couple goto statements to proceed.

Upvotes: 0

Views: 48

Answers (1)

Magoo
Magoo

Reputation: 80023

   exit goto :stopSuccess

Means "exit cmd" so as soon as "STOPPED" is detected, the procedure exits.

Drop the exit and it will go to :stopsuccess as expected.

Upvotes: 1

Related Questions