Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait For Commands to Finish

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.

like image 884
anderson Avatar asked Aug 04 '11 18:08

anderson


People also ask

Does bash wait for command to finish?

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.

What is the use of wait for command?

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.

What is wait command in shell?

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.

What is wait $!?

The & instructs bash to run the prior pipeline in a background subshell. The wait $! then waits for the pipeline to finish before returning.


1 Answers

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
like image 80
Aacini Avatar answered Sep 28 '22 03:09

Aacini