Search code examples
bashparallel-processing

How do you run multiple programs in parallel from a bash script?


I am trying to write a .sh file that runs many programs simultaneously

I tried this

prog1 
prog2

But that runs prog1 then waits until prog1 ends and then starts prog2...

So how can I run them in parallel?


Solution

  • To run multiple programs in parallel:

    prog1 &
    prog2 &
    

    If you need your script to wait for the programs to finish, you can add:

    wait
    

    at the point where you want the script to wait for them. For example:

    prog1 &
    prog2 &
    # (do other things while 'prog1' and 'prog2' carry on
    # in the background)
    ...
    # (Now we need the results of 'prog1' and 'prog2'
    # before continuing, so let's wait for them to
    # finish)
    wait