38

I have script including multiple commands. How can I group commands to run together ( I want to make several groups of commands. Within each group, the commands should run in parallel (at the same time). The groups should run sequentially, waiting for one group to finish before starting the next group) ... i.e.

#!/bin/bash
command #1
command #2
command #3
command #4
command #5
command #6
command #7
command #8
command #9
command #10

how can I run every 3 commands to gether? I tried:

#!/bin/bash
{
command #1
command #2
command #3
} & 
{   
command #4
command #5
command #6
} & 
{
command #7
command #8
command #9
}&
command #10

But this didn't work properly ( I want to run the groups of commands in parallel at the same time. Also I need to wait for the first group to finish before running the next group)

The script is exiting with an error message!

7
  • 2
    What do you mean, run together. Do you mean run in parallel at the same time? How do you know it "didn't work properly?" How will you know when it does work properly? Do you want to wait for the first group to finish before running the next group?
    – RobertL
    Nov 10, 2015 at 11:01
  • @RobertL.Yes I mean in parallel at the same time. The script is exiting with an error message. Yes I need to wait for the firdst group to finish before running the next group.
    – user88036
    Nov 10, 2015 at 11:04
  • 1
    @goro quoting you, the script is exiting with an error message. Please edit your question to add such message (for completion's sake) and remove the chit-chat (advices appreciated). Nov 10, 2015 at 11:06
  • Is it the same command running in each instance? Same name?
    – RobertL
    Nov 10, 2015 at 11:07
  • 1
    @goro, Is this accurate? "You have several groups of commands. Within each group, the commands should run in parallel (at the same time). The groups should run sequentially, waiting for one group to finish before starting the next group."
    – RobertL
    Nov 10, 2015 at 11:43

2 Answers 2

57

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
$   
4
  • 1
    I think the curlies are totally meaningless in your example, though. Wait is process-global, and the curlied groups won't create a new fork in this case. Nov 10, 2015 at 11:15
  • 2
    @PSkocik, True. Vestigial organs.
    – RobertL
    Nov 10, 2015 at 11:17
  • how can we run a script which never exits, wait for another script to exit, then run another script that never exits?
    – Dominic
    Jun 22 at 21:29
  • Great solution, but is there a way to collect returncodes of theses paralell running jobs? To be exact, important for me is if all returned zero, or not.
    – roli
    Jul 6 at 14:15
13
{
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.

1
  • 2
    The original question said only "run every 3 commands together". Without clarification, either answer is correct. Good call on the wait.
    – RobertL
    Nov 10, 2015 at 11:50

You must log in to answer this question.