Computer optimizations

Wait for the end of a process in a Batch file


In a batch process which calls an executable, you may need to prevent this executable from being launched more than once if the batch is restarted.

It is then interesting to wait for the end of the process or to cancel the call.

The function below, to be included in the calling Batch, allows you to check if a process is not running and, if so, to wait X seconds before restarting the check.

REM Check if a process is running.
    set executable=%1
    set waiting_time_in_seconds=%2
    set arguments=%3
    if [%arguments%]==[] (
        set arguments=%executable%
    WMIC process where "name like '%executable%'" get commandLine | find /i "%arguments%"
    IF "%ERRORLEVEL%"=="0" (
        echo %executable% is running...
        timeout %waiting_time_in_seconds%
        goto :check_running_process
    ) else (
        EXIT /B

It takes 3 parameters as input:

  • Process's file to search
  • The waiting time between 2 checks
  • Optionally the arguments to search

Use cases

This example checks every 20 seconds that the Windows calculator is not displayed, as in the screenshot below:

call :check_running_process calculator.exe 20

This other example checks every 5 seconds that the Java program "program.jar" is not running:

call :check_running_process java.exe 5 program.jar
Dernière modification le 15/04/2021 - Quillevere.net


No inscription needed if you wish to

Search in this website

fr en rss RSS info Informations