For loop in parallel

Hello,

My script shell is:

for i in $(seq $nb_lignes)

  do
 //command java
  done

Please, how can i execute all iteration in parallel ?

Thank you so much.

You can use threads. Since it's a java command that you want to run, you can write a small wrapper java program that calls the required methods and invoke threads that can run them in parallel. And you would want to rethink your strategy, if $nb_lines is going to be huge number!

for i in $(seq $nb_lignes)
do
//command java   &
done
wait
1 Like

i try

//command java &
done
wait

but i have a pb
each //command java is executed on a machine (i have 2 machines a and b and $nb_lignes=2)
i run the script on machine c , the fist iteration is well but i should do ctrl+c to pass to the second iteration or i want them in parall�ll without doing ctrl+c

have you an idea please ?

What do you mean you should do ctrl-c? What actually happens?

Here is my problem:

for i in $(seq $nb_lignes) # a list of machines
do
 machine=`head $1 -n $i | tail -1`
ssh root@$machine -x "java ....."
done

i have three machines: A,B and C
"A " run the script
list of machines contain B and C
i'd like to execute java command in the two machines B and C , A display that B run the command only
And when i do ctrl+C it display that C run the command

That sounds like the exact opposite of what you asked for before -- you want them to run in parallel, but you also want them to wait? Pick one.

Also, that is a horribly inefficient way of reading a file line by line...

while read LINE
do
        echo "line is $LINE"
done <$1

i want execute ssh root@$machine1 -x "java ....." and wait 10 seconds for example and execute
ssh root@$machine2 -x "java ....." automatically without the ctrl+C

in the terminal i have the result of command of machine 1 , i do ctrl+c it display the result of command of machine 2

I wait for your questions if it's not clear
thank you so much for help

So you want it to wait 10 seconds, then kill it automatically?

ssh root@$machineA -x "java ....." and wait 10 seconds and execute
ssh root@$machineB -x "java ....."

ssh root@$machineA -x "java ....." : create a node with Pastry overlay
wait 10 secondes
ssh root@$machineB -x "java .....":create another node join the first (that's way i have use sleep 10 secondes)

i run the script from machine C:
i'd like that it display : node 1 is created , wait 10 seconds and display node 2 is created

My problem: it display node 1 is created only

i tape ctrl+c it diplay node 2 is created

PS: the two process java are still runing in machine A and B
Thank you for help