18

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?

Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99
sudhakar m
  • 181
  • 1
  • 1
  • 3

4 Answers4

18

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
Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • This worked great for my needs. Thank you! What I also needed was to ensure those commands ran with success. So it looks more like: start "" 9>"%lock%1" CMD /C "one.bat > one-log.txt 2>&1 && echo success > one-success.txt" ... then, ensure that each -success.txt file exists, and type the output from each -log.txt. – macetw Dec 08 '15 at 18:29
  • @macetw i would like to see your solution that checks for success in a code block so that it is readable. Could you edit the answer above or submit your code block as an answer? – SketchBookGames Sep 20 '16 at 17:21
  • 2
    to wait you can use [`1>nul 2>nul timeout 1` instead of ping](http://stackoverflow.com/q/1672338/995714) – phuclv Mar 23 '17 at 04:01
  • 3
    @LưuVĩnhPhúc - Absolutely, though you might want to also throw in the `/nobreak`option. However, TIMEOUT is not available to XP, and that had a significant market share when this answer was written. Thankfully, XP is becoming less and less prevalent now that it is no longer supported. – dbenham Mar 23 '17 at 11:40
  • Regarding your wait line: `1>nul 2>nul ping /n 2 ::1` What does the `::1` do? – phonetagger Jun 15 '17 at 15:28
  • 1
    @phonetagger that's the [IPv6 loopback address](https://serverfault.com/q/193377/343888) – phuclv Jan 03 '18 at 03:48
6

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.

Jimbo
  • 61
  • 2
3

Create a master.bat file that starts one.bat and two.bat. When one.bat and two.bat end correctly, they echo to file they have finished

if errorlevel 0 echo ok>c:\temp\OKONE
if errorlevel 0 echo ok>c:\temp\OKTWO

Then the master.bat wait for the existence of the two files

del c:\temp\OKONE
del c:\temp\OKTWO
start one.bat
start two.bat
:waitloop
if not exist c:\temp\OKONE (
    sleep 5
    goto waitloop
    )
if not exist c:\temp\OKTWO (
    sleep 5
    goto waitloop
    )
start three.bat
start four.bat

Another way is to try with the /WAIT flag

start /WAIT one.bat
start /WAIT two.bat

but you don't have any control on errors.

Here's some references

http://malektips.com/xp_dos_0002.html

http://ss64.com/nt/sleep.html

http://ss64.com/nt/start.html

Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64
  • 4
    `start /WAIT xxx` waits for xxx to finish before letting execution pass to the next line in the batch file. So `one.bat` would have to finish before `two.bat` could even start. You COULD assume that `one.bat` will always finish first and use `start /wait two.bat`, but that's quite an assumption. BTW, you don't have to `start four.bat`, you could just `call four.bat`, (or even leave off the `call` if nothing else is to be done after executing `four.bat` ) and save the memory that would be used by launching another command processor. – James K Sep 26 '12 at 08:02
  • Marked as useful for the approach shared before the mention of start /WAIT. Also make sure you delete those files from master.bat before you start one.bat and two.bat. – Parag Doke Sep 27 '12 at 04:09
  • Thank you for the suggestion, edited the post accordingly :-) – Alessandro Da Rugna Sep 27 '12 at 06:55
1

Just adding another way, maybe the shortest.

(one.cmd | two.cmd) && (three.cmd | four.cmd)

Concept is really straight forward. Start one and 2 in paralel, once done and errorlevel is 0 run three and four.

Gerhard
  • 22,678
  • 7
  • 27
  • 43