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:
- Try to start each server's service via a for loop (I can get this working OK)
- If it fails to start try 'x' more times then move onto the next server - I am trying to use a nested for loop (I think correct terminology) for this. It sort of works - but I want it to try to start the service - then check if running - if it is - break out of loop and go to next server in array - if it fails to start the service try 'x' more times then move on... I can't really seem to make this part work properly.
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
CodePudding user response:
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="
)
)