I have a Windows batch file that starts other batch files. How do I wait for all the other batch files to finish before executing somthing in the first batch? I can't use /wait because I need the other commands to run in parallel.
The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.
wait command will suspend execution of the calling thread until one of its children terminate. It will return the exit status of that command. The sleep command is used to delay the execution of the next command for a given number of seconds, hours, minutes, days.
The wait command waits (pauses execution) until the process ID specified by the ProcessID variable terminates. If the ProcessID variable is not specified, the wait command waits until all process IDs known to the invoking shell have terminated and exit with a 0 exit status.
The & instructs bash to run the prior pipeline in a background subshell. The wait $! then waits for the pipeline to finish before returning.
You may use several flag files, one per running Batch file. For example:
First batch file create Flagfile.1 when enter, and delete it before end, and the same way the rest of concurrent batch files (Flagfile.2,...)
The main file just must wait for all flagfiles disappear. For example, in Main file:
start Batch1
start Batch2
.....
:wait
if exist flagfile.* goto wait
In any Batch file, for example Batch1:
echo %time% > flagfile.1
echo Do my process...
del flagfile.1
exit
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With