Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

executing batch files parallel and wait till all completes

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.

like image 609
user1190615 Avatar asked May 19 '26 20:05

user1190615


2 Answers

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,

like image 52
user1190615 Avatar answered May 21 '26 14:05

user1190615


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.

like image 40
SwenHahn Avatar answered May 21 '26 13:05

SwenHahn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!