[Batch] Wait For Running Process

Discussion in 'Scripting' started by demon.devin, Oct 2, 2017.

  1. demon.devin

    demon.devin MDL Novice

    Oct 2, 2017
    3
    5
    0
    Save the below code in a batch file, something like wait.bat or whatever.

    Code:
    @echo off
    goto begin
    
    :: INFO: Check to see if a process is running and if so wait before launching
    ::       a new process. After checking for a process this .bat file will wait
    ::       for a set amount of seconds declared be the user before checking and
    ::       looping again or moving onto the next command.
    
    :: EDIT: Change "seconds=" to "seconds=15000" to set a time of 15 seconds for
    ::       the wait period. For 5 seconds change it to "seconds=5000" For extra
    ::       commands you wish to add be sure to add them further below but it is
    ::       recommended that you know what you're doing. If you need help let me
    ::       know and I'll be happy to assist you. =)
    
    :begin
    
    :: Set wait time in seconds here
    set seconds=
    
    :: Set your 1st process to wait for here
    set program=
    
    :: Loop check
    echo.
    echo.
    echo Launching the %program% portable...
    
    start %program%
    
    :check
        cls
    
        set errorlevel=
    
        tasklist /fi "imagename eq %program%" | find /i "%program%"> NUL
    
        if /i %errorlevel% GTR 0 goto complete
    
        echo.
        echo The %program% process is running!
        echo.
        echo Will check again in a few seconds.
        echo.
        ping -n 1 -w %seconds% 1.1.1.1 > nul
        goto check
    
    :: Process complete. Start cleaning up!
    :complete
        echo.
        echo The %program% process has finished!
        echo.
        echo Starting next command/process...
        echo.
    :: Add here the next instruction you'd like to execute.
    
    :eof
     
  2. bear_aussie

    bear_aussie MDL Senior Member

    Jun 8, 2015
    271
    292
    10
    start /wait, mate :)
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. TairikuOkami

    TairikuOkami MDL Expert

    Mar 15, 2014
    1,172
    1,055
    60
    Never worked for me. :dunno:
     
  4. KNARZ

    KNARZ MDL Addicted

    Oct 9, 2012
    895
    482
    30
    start /wait depends on the process. - e.g. some packed exe installer with msi get return 0 instantly.
     
  5. Chibi ANUBIS

    Chibi ANUBIS MDL Chibi Developer

    Apr 28, 2014
    1,237
    911
    60
    I use this for wait a program running :

    Code:
    SETLOCAL EnableExtensions
    SET EXE=DINO.EXE
    ::::::::::::::::::::::::::::
    ::RUN GAME
    ::::::::::::::::::::::::::::
    :ChechIfRunning
    FOR /F %%x IN ('tasklist /NH /FI "IMAGENAME eq %EXE%"') DO IF %%x == %EXE% goto ProcessFound
    goto ProcessNotFound
    :ProcessFound
    cls
    ECHO.
    ECHO **************************************
    ECHO Don't close this Windows, Game is running
    ECHO **************************************
    ECHO.
    ECHO %EXE% is running
    timeout 1 > NUL
    goto ChechIfRunning
    :ProcessNotFound
    cls
    ECHO.
    ECHO **************************************
    ECHO Don't close this Windows, Game is not running
    ECHO **************************************
    
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...