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.
Parallel Batch with loop back to Parent
Moderator: DosItHelp
Re: Parallel Batch with loop back to Parent
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,
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,
Re: Parallel Batch with loop back to Parent
I would do it in a less complicated way:
Batch script "Daily.bat":
Batch script "daily1.bat":
Batch script "daily2.bat":
Batch script "daily3.bat":
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 .
penpen
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 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 .
penpen
Re: Parallel Batch with loop back to Parent
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
Substitute a PING delay if your machine does not have TIMEOUT.
Dave Benham
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
Dave Benham
-
- Posts: 13
- Joined: 26 Sep 2012 12:08
Re: Parallel Batch with loop back to Parent
These suggestions all work, or you can look into the start \wait flag which stops completely until the executed script is completed.
Re: Parallel Batch with loop back to Parent
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
Re: Parallel Batch with loop back to Parent
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.
I will try out the one from Dave given that for the parallel batches I can't guarantee in what order they will finish.