37

I want to write a bash script where I run two commands simultaneously, then continue when they both complete.

Here's something that doesn't work, but I'll put it here to illustrate what I'm trying to do:

#!/bin/bash
./job1 &
./job2
./dostuffwithresults

The script will run both job1 and job2 at the same time, but will only wait for job2 to finish before continuing. If job1 takes longer, then the results might not be ready for the final command.

Colin
  • 10,447
  • 11
  • 46
  • 54

2 Answers2

64
j1 &
j2 &
j3 &
wait $(jobs -p)
dostuffwithresults
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

something like this should work

    #!/bin/bash
    while [ `pgrep job*` ]
    do 
    echo 'waiting'
    done

    ./dostuffwithresults
Harry Forbess
  • 2,094
  • 4
  • 16
  • 15