D
D
Djasar2020-12-26 23:49:39
cmd/bat
Djasar, 2020-12-26 23:49:39

Need to loop a service until it starts without errors?

It is not possible to add a condition to the bat file, it is necessary that the service first stop without errors, and then start in the same way, if it does not start, then loop until it starts. when started just close the window.

net stop PeerDistSvc
if %errorlevel% == 2 echo Could not stop service.
if %errorlevel% == 0 echo Service stop successfully.
echo Errorlevel: %errorlevel%
net start PeerDistSvc
if %errorlevel% == 2 echo Could not start service.
if %errorlevel% == 0 echo Service start successfully.
echo Errorlevel: %errorlevel%

Thanks for any ideas on how to do this. desirable code.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Djasar, 2020-12-27
@Djasar

A LITTLE TORTURE
Everything worked out !!! HURRAH. Check if the solution is correct.
Condition:
1. The service needs to be restarted
2. If the service is stopped, then try to start it anyway
3. If the service is running, then stop it until it stops and then start it until it starts.

@ECHO OFF
sc query "Peer" %1 | findstr RUNNING
if %ERRORLEVEL% == 2 goto trouble
if %ERRORLEVEL% == 1 goto stopped
if %ERRORLEVEL% == 0 goto started
echo unknown status
goto end
:trouble
net stop Peer
if %errorlevel% == 2 goto trouble
if %errorlevel% == 0 goto stopped
:started
net stop Peer
if %errorlevel% == 2 goto trouble
if %errorlevel% == 0 goto stopped
:stopped
net start Peer
if %errorlevel% == 2 goto stopped
if %errorlevel% == 0 goto end
:end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question