Elad Weiss
Elad Weiss

Reputation: 4002

Run unknown number of processes (for loop) and wait for them to finish in bash

I have a bash script which does the following:

for server in $servers; do  # servers from input
     ssh $server <some long running command...>
done

# Rest of the code...

I would like to parallelize the ssh calls, but only continue to the rest of the code after all the servers have finished. I noticed similar questions here but all had either a fixed number of processes, or did not wait for finish. I would also accept an answer like don't do it in bash!. Thanks.

Upvotes: 0

Views: 41

Answers (1)

Cyrus
Cyrus

Reputation: 88969

Replace

ssh $server <some long running command...>

by

ssh $server <some long running command...> &

and replace

done

by

done
wait

Upvotes: 3

Related Questions