windows - How to wait for a process to terminate to execute another process in batch file

Windows - How to wait for a process to terminate to execute another process in batch file

You can use the timeout command in a batch file to pause the execution and wait for a specific duration. You can combine this with a loop to continuously check whether a process is still running. Here's an example:

@echo off set "processName=your_process.exe" set "timeoutInSeconds=5" :CHECK_PROCESS tasklist | find /i "%processName%" > nul if errorlevel 1 ( echo The process %processName% is not running. echo Starting another process... start "New Process" your_new_process.exe ) else ( echo The process %processName% is still running. Waiting... timeout /nobreak /t %timeoutInSeconds% goto CHECK_PROCESS ) echo Script execution completed. 

Replace your_process.exe with the name of the process you want to wait for, and your_new_process.exe with the new process you want to start after the first process has terminated.

Explanation:

  • The script checks whether the specified process is running using tasklist and find.
  • If the process is not found (errorlevel 1), it means the process has terminated, and the script proceeds to start another process (your_new_process.exe).
  • If the process is still running, it echoes a message, waits for a specified duration (timeoutInSeconds), and then goes back to checking the process status.

Save this script with a .bat extension and run it. Adjust the process names and timeout duration based on your requirements.

Examples

  1. "Batch script wait for a process to finish before starting another"

    • Batch Script Code:
      @echo off start /wait first_process.exe second_process.exe 
    • Description: Using start /wait to launch the first process and wait for it to finish before starting the second process.
  2. "Windows batch file wait for command to complete"

    • Batch Script Code:
      @echo off call :waitForProcess first_process.exe second_process.exe exit /b :waitForProcess tasklist | find /i "%1" > nul if errorlevel 1 ( goto :eof ) else ( timeout /t 1 /nobreak > nul goto waitForProcess ) 
    • Description: Defining a subroutine to wait for a specific process (first_process.exe) to finish before proceeding with the next process.
  3. "Batch file wait for background process"

    • Batch Script Code:
      @echo off start first_process.exe timeout /t 5 /nobreak > nul second_process.exe 
    • Description: Starting the first process and waiting for a specified time using timeout before launching the second process.
  4. "Windows batch script wait for command to complete with tasklist"

    • Batch Script Code:
      @echo off call :waitForProcess first_process.exe second_process.exe exit /b :waitForProcess :retry tasklist | find /i "%1" > nul if errorlevel 1 ( goto :eof ) else ( timeout /t 1 /nobreak > nul goto retry ) 
    • Description: Using a label and goto to continuously check if the first process is still running before proceeding to the second process.
  5. "Batch script wait for process to finish without timeout"

    • Batch Script Code:
      @echo off start /wait cmd /c first_process.exe second_process.exe 
    • Description: Using start /wait with cmd /c to run the first process and wait for it to finish before launching the second process.
  6. "Windows batch file wait for multiple processes to complete"

    • Batch Script Code:
      @echo off call :waitForProcesses "first_process.exe second_process.exe" third_process.exe exit /b :waitForProcesses :retry for %%P in (%1) do ( tasklist | find /i "%%P" > nul if errorlevel 1 ( goto :eof ) ) timeout /t 1 /nobreak > nul goto retry 
    • Description: Waiting for multiple processes to finish by checking each one in a loop before proceeding with the next process.
  7. "Batch script wait for process and handle errors"

    • Batch Script Code:
      @echo off start /wait first_process.exe || ( echo Error: First process failed. exit /b 1 ) second_process.exe 
    • Description: Using start /wait and checking the exit code to handle errors and proceed to the next process.
  8. "Windows batch file wait for process and log output"

    • Batch Script Code:
      @echo off start /wait first_process.exe > output.log 2>&1 second_process.exe 
    • Description: Redirecting the output of the first process to a log file and waiting for it to finish before launching the second process.
  9. "Batch script wait for process and show progress"

    • Batch Script Code:
      @echo off echo Waiting for first process to finish... start /wait first_process.exe echo First process completed. Starting second process. second_process.exe 
    • Description: Displaying a message to indicate waiting for the first process and providing progress information.
  10. "Windows batch file wait for process and retry on failure"

    • Batch Script Code:
      @echo off call :waitForProcess first_process.exe if %errorlevel% neq 0 ( echo First process failed. Retrying... call :waitForProcess first_process.exe ) second_process.exe exit /b :waitForProcess start /wait /b /min cmd /c %* || exit /b %errorlevel% exit /b 0 
    • Description: Defining a subroutine to wait for a process and retrying on failure before proceeding with the next process.

More Tags

top-n docker-desktop space-analysis abi pyqt5 angularjs-ng-click linq-to-json class-library checked graph

More Programming Questions

More Various Measurements Units Calculators

More Pregnancy Calculators

More Everyday Utility Calculators

More Tax and Salary Calculators