#!/bin/bash some_array=($1) echo "-- Setting-Up VM --" for i in ${some_array[@]}; do echo "VM #: $i" case "$i" in "1") echo "Setting-Up VM $i" sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@10.xx.x.xx <<EOF pwd nohup yes | /etc/rc.d/init.d/lifconfig su tarts nohup yes | vncserver sleep 10 exit exit EOF ;; "2") echo "Setting-Up VM $i" sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@10.xx.x.xx <<EOF pwd nohup yes | /etc/rc.d/init.d/lifconfig su tarts nohup yes | vncserver sleep 10 exit exit EOF ;; "3") echo "Setting-Up VM $i" sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@10.xx.x.xx <<EOF pwd nohup yes | /etc/rc.d/init.d/lifconfig su tarts nohup yes | vncserver sleep 10 exit exit EOF ;; *) echo "unknown VM!" ;; esac done
Can someone please guide me I have the above script which is executed when we run the script for instance ./vmSetup.sh "1 2 3"
but this is executed sequentially. I had created this script but now I want to run the cases in the script i.e. 1, 2 and 3 in parallel. Can someone also tell me how to run for instance 8 cases in parallel?
Advertisement
Answer
Put each case ... esac
statement in the background by ending it with &
. Then use wait
after the loop to wait for all the background processes to finish.
#!/bin/bash some_array=($1) echo "-- Setting-Up VM --" for i in ${some_array[@]}; do echo "VM #: $i" case "$i" in "1") echo "Setting-Up VM $i" sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@10.xx.x.xx <<EOF pwd nohup yes | /etc/rc.d/init.d/lifconfig su tarts nohup yes | vncserver sleep 10 exit exit EOF ;; "2") echo "Setting-Up VM $i" sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@10.xx.x.xx <<EOF pwd nohup yes | /etc/rc.d/init.d/lifconfig su tarts nohup yes | vncserver sleep 10 exit exit EOF ;; "3") echo "Setting-Up VM $i" sshpass -p "root12" ssh -tt -o StrictHostKeyChecking=no root@10.xx.x.xx <<EOF pwd nohup yes | /etc/rc.d/init.d/lifconfig su tarts nohup yes | vncserver sleep 10 exit exit EOF ;; *) echo "unknown VM!" ;; esac & done wait