Cant seem to figure this one out - hope someone can provide advice
I have a batch file that has an array of servers and loops through these to stop then start a Windows service
The stop part is working fine and the array of server names and service etc is all working as expected - so I will skip that for now.
The start part is what is causing me problems
What I want to happen is:
This is the code at present that sort of works but won't jump out of the nested loop if the service actually is started successfully. I have commented out the errorlevel check that I was trying to use to make it exit the loop if the service check did not error
rem Start service
echo 3. Starting "%service1%" Services >> %LOGPath%
for /L %%n in (0,1,1) do (
echo Starting the "%service1%" service on !server[%%n]! >> %LOGPath%
sc \\!server[%%n]! start "%service1%" > NUL
rem Wait a specified time between each check
timeout /t %StartWaitLoopDelay% /nobreak
echo Service "%service1%" status on !server[%%n]! is: >> %LOGPath%
sc \\!server[%%n]! query "%service1%" | find /I "STATE" | find "RUNNING" >> %LOGPath%
if errorlevel 1 (
for /L %%a in (1,1,2) do (
echo The "%service1%" service failed to start on !server[%%n]! - attmepting restart %%a times >> %LOGPath%
sc \\!server[%%n]! start "%service1%" > NUL
rem Wait a specified time between each check
timeout /t %StartWaitLoopDelay% /nobreak
echo Service "%service1%" status on !server[%%n]! is now: >> %LOGPath%
sc \\!server[%%n]! query "%service1%" | find /I "STATE" | find "RUNNING" >> %LOGPath%
rem if %errorlevel% EQU 0 goto ExitStartLoop
)
)
rem :ExitStartLoop
)
I have also tried the below using a Call (to another restart loop - then exit /b to try and get out of it) in the first loop if the service fails to start - also sort of works but not really - doesn't actually exit properly and prints the first loop code to output in cmd window...
rem Start service
echo 3. Starting "%service1%" Services >> %LOGPath%
for /L %%n in (0,1,1) do (
echo Starting the "%service1%" service on !server[%%n]! >> %LOGPath%
sc \\!server[%%n]! start "%service1%" > NUL
rem Wait a specified time between each check
timeout /t %StartWaitLoopDelay% /nobreak
echo Service "%service1%" status on !server[%%n]! is: >> %LOGPath%
sc \\!server[%%n]! query "%service1%" | find /I "STATE" | find "RUNNING" >> %LOGPath%
if errorlevel 1 Call :RestartLoop
)
GoTo :End
:RestartLoop
for /L %%a in (1,1,2) do (
echo The "%service1%" service failed to start on !server[%%n]! - attmepting to restart %%a times >> %LOGPath%
sc \\!server[%%n]! start "%service1%" > NUL
rem Wait a specified time between each check
timeout /t %StartWaitLoopDelay% /nobreak
echo Service "%service1%" status on !server[%%n]! is now: >> %LOGPath%
sc \\!server[%%n]! query "%service1%" | find /I "STATE" | find "RUNNING" >> %LOGPath%
if %errorlevel% EQU 0 (exit /b)
)
:End
Hope this makes sense and someone can provide some advice. I know I am using errorlevel and %errorlevel% - will clean this up if/when I can get it working correctly
I suggest you to use this method. The new lines inserted by me have an uppercase REM comment before.
rem Start service
echo 3. Starting "%service1%" Services >> %LOGPath%
for /L %%n in (0,1,1) do (
echo Starting the "%service1%" service on !server[%%n]! >> %LOGPath%
sc \\!server[%%n]! start "%service1%" > NUL
rem Wait a specified time between each check
timeout /t %StartWaitLoopDelay% /nobreak
REM Clear "error" flag
set "anyError="
echo Service "%service1%" status on !server[%%n]! is: >> %LOGPath%
sc \\!server[%%n]! query "%service1%" | find /I "STATE" | find "RUNNING" >> %LOGPath%
REM If was an error, set "error" flag
if errorlevel 1 set "anyError=true"
for /L %%a in (1,1,2) do if defined anyError (
echo The "%service1%" service failed to start on !server[%%n]! - attmepting restart %%a times >> %LOGPath%
sc \\!server[%%n]! start "%service1%" > NUL
rem Wait a specified time between each check
timeout /t %StartWaitLoopDelay% /nobreak
echo Service "%service1%" status on !server[%%n]! is now: >> %LOGPath%
sc \\!server[%%n]! query "%service1%" | find /I "STATE" | find "RUNNING" >> %LOGPath%
REM If was not an error, clear "error" flag
if not errorlevel 1 set "anyError="
)
)