1

I have an array with hostnames and I want to execute an other script with all the hostnames parallel. At first I did it iterative with a for-loop. But I want it parallel and I used the wait-command, but then I can't have dynamic number of hosts.

./sub.sh ${arr[0]} &
./sub.sh ${arr[1]} &
./sub.sh ${arr[2]}
wait

Now I need a function which with I can execute all the elements of the array. Do you have any solution for me?

0

2 Answers 2

3

With GNU Parallel like this:

# Generate test data
arr=($(seq 10))

# Check it looks good
echo "${arr[@]}"
1 2 3 4 5 6 7 8 9 10

# Process all elements of the array - just using "echo"
parallel -k echo {} ::: "${arr[@]}"
1
2
3
4
5
6
7
8
9
10

Other useful options, for parallel parameters:

  • -j N will run N jobs at a time
  • --progress will show a progress bar
  • -k will keep the outputs in order
  • --tag will tag outputs (according to host in your case)

In case the above is not clear in how it applies to your specific example, I am suggesting you do:

parallel --tag -k ./sub.sh {} ::: "${arr[@]}"
Sign up to request clarification or add additional context in comments.

Comments

2

Try the below:

for host in "${arr[@]}" ; do
   ./sub.sh "$host" &
done

# wait for the background processes to finish
wait

1 Comment

also - keep in mind that if you array is VERY big - this will have significant performance issues on your system

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.