I have a batch file that frequently restarts a group of services developed in house. We are soon moving away from these to something more stable, but for the time being the services need to be restarted pretty often in order to work properly.

To simplify this I have a batch file that runs on a schedule. It looks like this:

SETLOCAL EnableDelayedExpansion
ECHO ---------------------------------------------->> log.txt
ECHO -----------========================----------->> log.txt
ECHO -----------=========%date%=========----------->> log.txt
ECHO -----------========================----------->> log.txt
ECHO ---------------------------------------------->> log.txt

set DD=0
Set TIMESTAMP="eol=; tokens=1,2,3,4* delims=/, "
For /F %TIMESTAMP% %%i in ('DATE /t') DO (
  SET YYYYMMDD=%%l%%j%%k
  SET YYYYMM=%%l%%j
  set DD=%%k

echo A >> ServiceNames
echo B >> ServiceNames
echo C >> ServiceNames
echo D >> ServiceNames
echo E >> ServiceNames
echo F >> ServiceNames

for /f %%a in (ServiceNames) do (
    set timestamp=!date!!time!
    ECHO !timestamp! - Stopping Service %%a >> log.txt
    net stop /y srvService_%%a >> log.txt
    taskkill /f /im Service_%%a.exe >> log.txt

    ping localhost -n 15 > nul

    set timestamp=!date!!time!
    ECHO !timestamp! - Starting Service %%a >> log.txt
    net start srvService_%%a >> log.txt
if exist ServiceNames del ServiceNames

The problem is that on a couple of occasions, the batch file won't wait for one service to completely finish starting before stopping the next service.

Do you think adding an additional ping localhost command after the net start command might help?

3 Answers 3


You can use code like the following to keep waiting till the service (in this case I've tested with w32time) is started:

timeout /t 5 > NUL
for /f "tokens=4" %%s in ('sc query w32time ^| find "STATE"') do if NOT "%%s"=="RUNNING" goto WAIT
echo Service is now running!
  • This doesn't work.
    – AgentFire
    Oct 15, 2020 at 8:51

If there is no cleanup done when stopping, and they are running in their own process, just kill the process.

Ping is not the correct way to have a bit of a sleep. It loads the network. You have 3 choices. All have various options to show/hide stuff.

timeout /t 5


choice /d y /t 5


waitfor /t 5 FredToMarryGinger

net start will wait for the service to finish starting before returning, so if the batch file moves on before the service finishes starting, the issue is with the service falsely reporting it's started when it has not, or taking so long the command times out.

Ideally the service should be fixed to not report it has finished starting until it has finished starting. You could also have the service output a statefile or something to indicate it has finished starting and have your batch file wait for that to appear before moving on. Or you could create a loop to continuously re-check the service status with "net start" or "sc query" until it is showing as "Running", if timeouts are the problem.

[Edit] What Karan said.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .