0

I created this script inspired by this answer about how to wait for parralel batch scripts to finish and created this.

@echo off

set "lock=%temp%\wait%random%.lock"

start "1" /D .\RESULTS\1 cmd /c  9>"%lock%0" "1.exe"
start "2" /D .\RESULTS\2 cmd /c  9>"%lock%1" "2.exe"
start "3" /D .\RESULTS\3 cmd /c  9>"%lock%2" "3.exe"
start "4" /D .\RESULTS\4 cmd /c  9>"%lock%3" "4.exe"

:Wait0 for all scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (0 1 3) do (
  setlocal enabledelayedexpansion
  set L=!!lock!%%N!
  ( rem
  ) 9>!L! || goto :Wait0
) 2>nul

timeout /t 10 /nobreak
echo 1 done
echo 2 done
echo 3 done
echo 4 done





start "5" /D .\RESULTS\5 cmd /c  9>"%lock%4" "5.exe"
start "6" /D .\RESULTS\6 cmd /c  9>"%lock%5" "6.exe"
start "7" /D .\RESULTS\7 cmd /c  9>"%lock%6" "7.exe"
start "8" /D .\RESULTS\8 cmd /c  9>"%lock%7" "8.exe"

:Wait1 for all scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (4 1 7) do (
  setlocal enabledelayedexpansion
  set L=!!lock!%%N!
  ( rem
  ) 9>!L! || goto :Wait1
) 2>nul

timeout /t 10 /nobreak
echo 5 done
echo 6 done
echo 7 done
echo 8 done




start "9" /D .\RESULTS\9 cmd /c  9>"%lock%8" "9.exe"
start "10" /D .\RESULTS\10 cmd /c  9>"%lock%9" "10.exe"

:Wait2 for all scripts to finish (wait until lock files are no longer locked)
1>nul 2>nul ping /n 2 ::1
for %%N in (8 1 9) do (
  setlocal enabledelayedexpansion
  set L=!!lock!%%N!
  ( rem
  ) 9>!L! || goto :Wait2
) 2>nul

timeout /t 10 /nobreak
echo 9 done
echo 10 done


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

In the beginning seems like everything is working fine with 1.exe 2.exe 3.exe 4.exe Loop is waiting while all of them finish working and only then proceeding. The problem starts in the 2nd loop with 5.exe 6.exe 7.exe 8.exe. Then after 2 or 3 programs finish it's work loop exits and doesn't wait for last program to finish it's job. Any1 can see the error in this script except that this probably is not the most efficient way to write this?

etrusks
  • 165
  • 1
  • 8

1 Answers1

0

Well I tryed to understand the magic behind that lock thing and saw that it was a .lock file itself that did a trick. So instead of set "lock=%temp%\wait%random%.lock" I created something simpler

echo. 2>lock0.lock
echo. 2>lock1.lock
echo. 2>lock2.lock
echo. 2>lock3.lock

Just 4 lock files coz I'm only planning to run 4 .exe files at once. Then changed these lines

start "1" /D .\RESULTS\1 cmd /c  9>"%lock%0" "1.exe"
start "2" /D .\RESULTS\2 cmd /c  9>"%lock%1" "2.exe"
start "3" /D .\RESULTS\3 cmd /c  9>"%lock%2" "3.exe"
start "4" /D .\RESULTS\4 cmd /c  9>"%lock%3" "4.exe"

to

start "1" /D .\RESULTS\1 cmd /c  9>"lock0.lock" "1.exe"
start "2" /D .\RESULTS\2 cmd /c  9>"lock1.lock" "2.exe"
start "3" /D .\RESULTS\3 cmd /c  9>"lock2.lock" "3.exe"
start "4" /D .\RESULTS\4 cmd /c  9>"lock3.lock" "4.exe"

and changed inside of loop to

( rem
) 9>"lock0.lock" || goto :Wait0
( rem
) 9>"lock1.lock" || goto :Wait0
( rem
) 9>"lock2.lock" || goto :Wait0
( rem
) 9>"lock3.lock" || goto :Wait0

And in the end delete all lock files

del "lock0.lock"
del "lock1.lock"
del "lock2.lock"
del "lock3.lock"

I know this is a dirty solution but maybe someone might find this helpful.

etrusks
  • 165
  • 1
  • 8