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

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.

2
  • 1
    You explicitly have an EXIT command. The GOTO :stopsuccess will not execute because you are using EXIT in front of it. Commented Mar 3, 2017 at 14:35
  • 1
    And just so you know, you cannot forcefully break out of a FOR /L command. It will execute all 5 times regardless of the IF statement being TRUE. Commented Mar 3, 2017 at 14:41

1 Answer 1

1
   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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.