0

I got a script that is starting up some virtual machines. After the deployment I want to install a few things on the VMs. Because these installation can take up to 6 minutes per VM. It would be much more efficient to execute these installations in parallel. In Java I would probably use Threads but in a bash script I do not know. My first approach was sth like this:

function install {
    plink -ssh -i /var/lib/one/Downloads/id_rsa_ubuntu_putty..ppk root@$1 wget https://www.dropbox.com/s/xdhnx/install.sh
    plink -ssh -i /var/lib/one/Downloads/id_rsa_ubuntu_putty..ppk root@$1 chmod 4500 install.sh
    plink -ssh -i /var/lib/one/Downloads/id_rsa_ubuntu_putty..ppk root@$1 ./install.sh

    echo $1 angestoßen
    }   

echo -------------------------------------------------
echo Alle VMs erfolgreich deployed

for i in "${IParray[@]}"
do
    install $i &
done
wait

I created a funtion and tried to connect the function calls in the for-loop by using "&" which should create subprocesses. But for some how this is not working properly. Can anybody help me out

Brave
  • 177
  • 5
  • 15
  • 2
    In what way is it not working properly? – Rick Jun 29 '15 at 13:05
  • I tested it several times, but only one vm is getting the installations.... Is there sth. wrong with my attempt? – Brave Jun 29 '15 at 15:10
  • When I get replace the plink commands with a sleep your script works as expected (i.e. sleeps for n seconds and then prints all of the IPs at once) so that means it probably has something to do with the plink/ssh. – Rick Jun 29 '15 at 17:45

1 Answers1

0

Maybe use GNU Parallel like this:

#!/bin/bash

IParray=(192.168.0.1 192.168.0.2)

function install {
   echo $1
   # plink...
}

# Make install() visible to GNU Parallel
export -f install

# Run a bunch of installs in parallel
parallel install ::: ${IParray[@]}
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks for your support. If I run that I get: `FATAL ERROR: No route to host` But I can ping the machines afterwards... So that there obviously is a route to the host. – Brave Jun 29 '15 at 13:57
  • My IP addresses are only examples - I intended for you to use your own. – Mark Setchell Jun 30 '15 at 09:35
  • Haha ofc, I did try it with my own :P maybe that error occurs due to the host key verification... – Brave Jun 30 '15 at 10:43
  • For this to work for me on Debian 7, I had to use the --gnu option of parallel. Also see http://stackoverflow.com/questions/23814360/gnu-parallel-and-bash-functions-how-to-run-the-simple-example-from-the-manual – Chris Dec 05 '16 at 23:22