I have a main batch file which calls multiple batch files. I want to be able to execute all these batch files at the same time. Once they are all done, I have further processes that needs to carry on in the main match file.
When I use 'Start' to call the multiple batch files, able to kick off all batch files concurrently but I lose tracking them. (Main batch file thinks their processes are done the moment it executes other batch files).
When I use 'Call', able to monitor the batch file process, but it kicks off the batch files sequentially instead of concurrently.
Is there a way around this? Have limited permissions on this PC and trying to accomplish this using Batch only.
Main Batch file
call first.bat
call second.bat
call third.bat
:: echo only after all batch process done
echo done!
first.bat
timeout /t 10
second.bat
timeout /t 10
third.bat
timeout /t 10
This will generate a temporary file and lock it by creating a redirection to it, starting the batch subprocesses inside this redirection. When all the subprocesses end the redirection is closed and the temporary file is deleted.
While the subprocesses are running, the file is locked, and we can test this trying to rename the file. If we can rename the file, subprocesses have ended, else some of the processes are still running.
note: any process started inside the subprocesses will also hold the redirection and the lock. If your code leaves something running, this can not be used.
This is the simplest and most efficient way to solve this problem:
In this method the waiting state in the main file is event driven, so it does not consume any CPU time. The
pause
command would terminate when anyone of the commands in the( block )
outputs a character, butstart
commands don't show any output in this cmd.exe. In this way,pause
keeps waiting for a char until all processes started bystart
commands ends. At that point the pipe line associated to the( block )
is closed, so thepause
Stdin is closed and the command is terminated bycmd.exe
.Where the batches are constructed like
That is, each called batch first creates a file in the temporary directory, then deletes it after the required process is run. The filename to create/delete is provided as the first parameter to the batch and "quoted" because the
temp
directoryname typically contains separators.The mainline simply creates a temporary directory and invokes the subprocedures, then repeatedly waits 1 second and checks whether the subprocedures' flagfile have all been deleted. Only if they have all been deleted with the procedure continue to delete the temporary directory
Adding to the answer by Aacini. I was also looking for similar task. Objective was to run multiple commands parallel and extract output (stdout & error) of all parallel processes. Then wait for all parallel processes to finish and execute another command. Following is a sample code for BAT file, can be executed in CMD:
All the statements inside
()
are executed first, wait for them to complete, then last two lines are executed. All commands begining withstart
are executed simultaneously.