Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for parallel batch scripts

I have 4 batch files. I want to run one.bat and two.bat at once, concurrently. After completion of these two batch files, three.bat and four.bat should run at once, in parallel. I tried with many ways but mot works fine.

Can anyone help me over this?

like image 514
sudhakar m Avatar asked Sep 25 '12 06:09

sudhakar m


2 Answers

This is easily done using a much simplified version of a solution I provided for Parallel execution of shell processes. Refer to that solution for an explanation of how the file locking works.

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

:: Launch one and two asynchronously, with stream 9 redirected to a lock file.
:: The lock file will remain locked until the script ends.
start "" cmd /c 9>"%lock%1" one.bat
start "" cmd /c 9>"%lock%2" two.bat

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

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

:: Launch three and four asynchronously
start "" cmd /c three.bat
start "" cmd /c four.bat
like image 116
dbenham Avatar answered Oct 14 '22 18:10

dbenham


I had this same dilemma. Here's the way I solved this issue. I used the Tasklist command to monitor whether the process is still running or not:

:Loop
tasklist /fi "IMAGENAME eq <AAA>" /fi "Windowtitle eq <BBB>"|findstr /i /C:"<CCC>" >nul && (
timeout /t 3
GOTO :Loop
)
echo one.bat has stopped
pause

You'll need to tweak the

<AAA>, <BBB>, <CCC>

values in the script so that it's correctly filtering for your process.

Hope that helps.

like image 30
Jimbo Avatar answered Oct 14 '22 17:10

Jimbo