I'd like to use gnu parallel to exec a function inside a bash script in a for loop and I'm not able to figure out how from the info I got online . For example:
#!/bin/bash
get_racks(){
query that outputs a list of racks
}
get_hosts() {
query that outputs a list of hosts by rack(passed as param to the func)
}
get_gw() {
query that outputs a list of gateways
}
check_ping() {
HOSTS=(`get_hosts`)
COUNTER=0
while [ $COUNTER -lt $SIZE ] ; do
ssh $HOSTS "ping -c 5 ${GATEWAYS[$COUNTER]} " ;
COUNTER=$((COUNTER+1)) ;
done
}
RACKS=(`get_racks`)
HOSTS=(`get_hosts`)
GWS=(`get_gw`)
SIZE=${#GWS[@]}
COUNTER=0
for rack in ${RACKS[@]}; do
parallel check_ping | tee output.txt
done
I'd like to execute the for loops and what happens inside in parallel while controlling the number of concurrent jobs and output everything into a single file . The final file should have all the data the function returns and also print to stdout . Also , the check_ping has a part of sshing to a remote machine and pinging a GW, this part happens actually in parallel with a custom ssh command we have that ssh to all the hosts in the array in parallel and pings a GW .
Thanks much