Parallel Batch with loop back to Parent

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vtpsu99
Posts: 2
Joined: 05 Sep 2013 14:03

Parallel Batch with loop back to Parent

#1 Post by vtpsu99 » 05 Sep 2013 14:18

Hi All,

Is there a way to create batch file such that it runs "child" batches in parallel but then when the last child is done it will return back to the "parent" batch file to finish executing other steps?

For example you can have a parent batch called Daily.bat with it having the following info:

start daily1.bat
..
start daily2.bat
..
start daily3.bat

but then after those 3 batch files finish completely there may be one last consolidated step in Daily.bat.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Parallel Batch with loop back to Parent

#2 Post by abc0502 » 05 Sep 2013 14:44

Hi , :)
It can be done but it will depend on what the last daily.bat file will do.
Let's say it will create a log file called "log3.log" After it finish it's work, so you code your main batch
to check for that file if it has been created or not, if it was it continue, else it sleep for a while.

I hope you get the idea.
by the way, you have a double post.

Regards,

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Parallel Batch with loop back to Parent

#3 Post by penpen » 05 Sep 2013 15:02

I would do it in a less complicated way:

Batch script "Daily.bat":

Code: Select all

@echo off
echo parent.bat start threads
(call daily1.bat) | (call daily2.bat) | (call daily3.bat)
echo parent.bat threads ended
echo parent.bat does something
echo finishing parent.bat

Batch script "daily1.bat":

Code: Select all

@echo off
ping 127.0.0.1 -n 3 2>nul >nul
(echo finishing daily1.bat) >&2

Batch script "daily2.bat":

Code: Select all

@echo off
ping 127.0.0.1 -n 2 2>nul >nul
(echo finishing daily1.bat) >&2

Batch script "daily3.bat":

Code: Select all

@echo off
ping 127.0.0.1 -n 1 2>nul >nul
(echo finishing daily1.bat) >&2
The daily1.bat, daily2.bat, daily3.bat are just for demonstrating.
The only bad thing is that you have to redirect all "echo's" to stderr ( >&2), if you want to display them,
else the output is passed via an anonymous pipe, and the sending process will be blocked,
if the pipe output buffer is full, but if you know it, that should be no problem :mrgreen: .

penpen

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Parallel Batch with loop back to Parent

#4 Post by dbenham » 05 Sep 2013 15:14

I like to use temporary lock files.

Have each started process redirect an unused handle to a lock file. The file will remain locked until the process is complete.

The main batch file attempts to open each lock file within a loop. If any of the opens fails, then it loops back, waits a bit, and tries again. Once it is able to open all 3 lock files, you know you are done. So delete the temp lock files and then do whatever additional cleanup is required.

I really like this solution because:
- It works with any script or executeable, without modification
- It does not interfere with stdin, stdout, or stderr
- The master script detects when the children terminate, regardless how they terminate

Code: Select all

@echo off

start cmd /c "daily1.bat 9>dailyLock1"
start cmd /c "daily2.bat 9>dailyLock2"
start cmd /c "daily3.bat 9>dailyLock3"

:wait
timeout /nobreak 1 >nul
2>nul (for /l %%N in (1 1 3) do ((>lock%%N call ) || goto wait ))

delete dailyLock*
:: Additional finishing code goes here
Substitute a PING delay if your machine does not have TIMEOUT.


Dave Benham

Jess Selien
Posts: 13
Joined: 26 Sep 2012 12:08

Re: Parallel Batch with loop back to Parent

#5 Post by Jess Selien » 05 Sep 2013 19:09

These suggestions all work, or you can look into the start \wait flag which stops completely until the executed script is completed.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Parallel Batch with loop back to Parent

#6 Post by dbenham » 05 Sep 2013 19:49

Jess Selien wrote:These suggestions all work, or you can look into the start \wait flag which stops completely until the executed script is completed.

But that defeats the whole point of the original question - the scripts will run sequentially instead of in parallel.


Dave Benham

vtpsu99
Posts: 2
Joined: 05 Sep 2013 14:03

Re: Parallel Batch with loop back to Parent

#7 Post by vtpsu99 » 06 Sep 2013 06:22

Thanks for the tips!

I will try out the one from Dave given that for the parallel batches I can't guarantee in what order they will finish.

Post Reply