executing batch files parallel and wait till all completes
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.
user1190615
Updated on June 05, 2022Comments
-
user1190615 almost 2 years
I have a
master batch
file which calls another listbatch
files. I want to wait until all thebatch
files are executed (cannot say which one will execute faster) and comes tomaster 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 filebuild.bat
andbuild.bat
files runs theweb & db bat
files in parallel and once they are executed, it has to return tomaster.bat
and run thepost.bat
.can any one please guide me how to do this.