Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 840
Betamoo Avatar asked Jun 09 '10 10:06

Betamoo


People also ask

How do you run two commands in parallel shell?

#!/bin/bash for cmd in "$@"; do { echo "Process \"$cmd\" started"; $cmd & pid=$! PID_LIST+=" $pid"; } done trap "kill $PID_LIST" SIGINT echo "Parallel processes have started"; wait $PID_LIST echo echo "All processes have completed"; Save this script as parallel_commands and make it executable.


1 Answers

How about:

prog1 & prog2 && fg 

This will:

  1. Start prog1.
  2. Send it to background, but keep printing its output.
  3. Start prog2, and keep it in foreground, so you can close it with ctrl-c.
  4. When you close prog2, you'll return to prog1's foreground, so you can also close it with ctrl-c.
like image 158
Ory Band Avatar answered Sep 27 '22 20:09

Ory Band