Run commands in parallel and wait for one group of commands to finish before starting the next
The commands within each group run in parallel, and the groups run sequentially, each group of parallel commands waiting for the previous group to finish before starting execution.
The following is a working example:
Assume 3 groups of commands as in the code below. In each group the three commands are started in the background with &
.
The 3 commands will be started almost at the same time and run in parallel while the script waits
for them to finish.
After all three commands in the the third group exit, command 10
will execute.
$ cat command_groups.sh
#!/bin/sh
command() {
echo $1 start
sleep $(( $1 & 03 )) # keep the seconds value within 0-3
echo $1 complete
}
echo First Group:
command 1 &
command 2 &
command 3 &
wait
echo Second Group:
command 4 &
command 5 &
command 6 &
wait
echo Third Group:
command 7 &
command 8 &
command 9 &
wait
echo Not really a group, no need for background/wait:
command 10
$ sh command_groups.sh
First Group:
1 start
2 start
3 start
1 complete
2 complete
3 complete
Second Group:
4 start
5 start
6 start
4 complete
5 complete
6 complete
Third Group:
7 start
8 start
9 start
8 complete
9 complete
7 complete
Not really a group, no need for background/wait:
10 start
10 complete
$
{
command #1
command #2
command #3
} &
{
command #4
command #5
command #6
} &
{
command #7
command #8
command #9
}&
command #10
wait #<===
Should work (each individual triplet component will run sequentially, but hte groups will run in parallel). You probably don't want your parent shell to exit before the groups have finished -- hence the wait
.