How to wait all batch files to finish before exiting?

*爱你&永不变心* 提交于 2019-11-26 02:19:56

I think this is the simplest and most efficient way:

@echo off

echo %time%

(
    start call batch1.bat
    start call batch2.bat
    start call batch3.bat
    start call batch4.bat
) | set /P "="

echo %time%

In this method the waiting state in the main file is event driven, so it does not consume any CPU time!

EDIT: Some explanations added

The set /P command would terminate when anyone of the commands in the ( block ) outputs a line, but start commands don't show any line in this cmd.exe. This way, set /P keeps waiting for input until all processes started by start commands ends. At that point the pipe line associated to the ( block ) is closed, so the set /P Stdin is closed and set /P command is terminated by the OS.

give a unique title string to the new processes, then check if any processes with this string in the title are running:

start "+++batch+++" batch1.bat
start "+++batch+++" batch2.bat
start "+++batch+++" batch3.bat
start "+++batch+++" batch4.bat
:loop
  timeout /t 1 >nul
  tasklist /fi "windowtitle eq +++batch+++*" |find "cmd.exe" >nul && goto :loop
echo all tasks finished

(find is used, because tasklist does not return a helpful errorlevel)

Give this a try.

@echo off
echo %time%
start "" /wait cmd /c bat1.bat |start "" /wait cmd /c bat2.bat |start "" /wait cmd /c bat3.bat
echo %time%

pause

You could have batch1..batchn4 create flag files when they finish running.

e.g echo y > flag1 in batch1.bat

Then in the main batch file check for the existence of the flag files before exiting. You would need some sort of sleep utility to do something like this at the end of the main batch file:

IF EXIST flag1 GOTO check2
sleep <for a short amount of time>
goto check1

:check2
IF EXIST flag2 GOTO check3
sleep <for a short amount of time>
goto check2

:check3
IF EXIST flag3 GOTO check4
sleep <for a short amount of time>
goto check3

:check4
IF EXIST flag4 GOTO xit
sleep <for a short amount of time>
goto check4

:xit

The downside of this technique is that your timing is going to be off a little because you're polling for the flag files instead of being event driven. This may or may not be a problem in your situation. Batch files are pretty limited in this way. You might be better off trying to do it in PowerShell or python or some other more capable scripting language.

Consider the ‘Job’ series of commands in Powershell as an alternative to using a batch script.

https://www.sconstantinou.com/powershell-jobs/

No one mentioned this solution, which I find to be the best. Put this at the end of your script, modify your process name if needed and the search strings.

This includes modifications necessary to have this work remotely too which was a headache. Originally I used tasklist instead of WMIC and checked named window titles defined when START is called, but windowName is N/A in tasklist when using it remotely. The powershell command worked better than timeout in our circumstances, which required us to run from this from a Jenkins agent.

    :WAIT_FOR_FINISH
        ECHO Waiting...
        powershell -command "Start-Sleep -Milliseconds 5000" > nul
        WMIC PROCESS WHERE Name="cmd.exe" GET commandline | findstr "searchString1 searchString 2" > nul && goto :WAIT_FOR_FINISH
        ECHO.
        ECHO Processes are Complete!
        ECHO.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!