executing batch files parallel and wait till all completes

14,780

Solution 1

Finally got the resolution but forgot to update :)

@echo off
setlocal
set "lock=%temp%\wait%random%.lock"

:: Launch processes asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" 9>"%lock%1" web.bat
start "" 9>"%lock%2" db.bat

:Wait for both processes to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (1 2) do (
  (call ) 9>"%lock%%%N" || goto :Wait
) 2>nul

::delete the lock files
del "%lock%*"

:: Finish up
echo Done - ready to continue processing.

Thanks,

Solution 2

If you don't mind using an additional tool – the Windows command processor doesn't support this "out of the box" – you can do it like this without the need for "busy waiting" or using "lock" files:

mparallel.exe --count=3 --shell first.bat : second.bat : third.bat
call post.bat

First line will start first.bat, second.bat and third.bat in parallel. It will not return until all three have terminated. We use --shell here to run tasks in a shell, which is required for BAT files. After the three BAT's have completed, post.bat is executed. Could be another mparallel call though.

Share:
14,780
user1190615
Author by

user1190615

Updated on June 05, 2022

Comments

  • user1190615
    user1190615 almost 2 years

    I have a master batch file which calls another list batch files. I want to wait until all the batch files are executed (cannot say which one will execute faster) and comes to master batch file to execute the remaining.

    Example:

               a) master.bat            b) build.bat
                  call build.bat           start web.bat        
                  post.bat                 start db.bat                            
    

    here master.bat calls a file build.bat and build.bat files runs the web & db bat files in parallel and once they are executed, it has to return to master.bat and run the post.bat.

    can any one please guide me how to do this.