Wait For Service To Stop in a Batch File

Sometimes in a batch file we need to wait until a service is stopped before we carry on processing our script.  NET STOP will only wait a finite amount of time for a service to stop, so for services that take a while to stop we can use a simple loop.  This is just an example of stopping the Adobe update service and waiting – just replace the service name as required:

@echo off

REM Run your command line here - probably: NET STOP AdobeARMservice 
 
REM Wait for Adobe Update service to stop
:LOOP
sc query AdobeARMservice | find "STOPPED" >nul 2>&1

IF ERRORLEVEL 1 (
  Timeout /T 5 /Nobreak
  GOTO LOOP
) ELSE (
  GOTO CONTINUE
)
 
:CONTINUE
 
REM Carry on with more logic

 

Wait For Process in a Batch File

A while ago I needed to wait for a process to end in a batch file, before the script continued.  I did it by using an If statement to create a ‘spoof’ loop, with a delay between each loop iteration of 5 seconds.  This example waits for the ‘Java.exe’ process to end before the script continues:

REM Run your command line here


REM Wait for spawned Java.exe process to end
:LOOP
tasklist | find /i "Java.exe" >nul 2>&1
IF ERRORLEVEL 1 (
  GOTO CONTINUE
) ELSE (
  Timeout /T 5 /Nobreak
  GOTO LOOP
)

:CONTINUE

REM Clean up shortcuts