please can someone point me in the right direction with a shell scripting problem.
I want to execute a command in a for loop and the command should be started not one-by-one meaning the for loop is waiting for the exit code , it should be started in parallel.
I have a plain text file where a hostname stands in each line.
#!/bin/bash
file="/tmp/serverlist"
for server in `cat $file` ; do
ssh admin@$server "command to run"
done
the problem is :
the command I execute with SSH takes approx. 10 minutes to complete.
I have to run the command on 145 servers and I dont want to wait 10 mins to go to the next. I want to execute it in parallel say on 10 servers at a time.
Any ideas ? simply putting a & after the SSH commands didnt work, nor with continue ?
ssh should be able to work as a background process. Maybe if you enclose the command you want to issue in double quotes to make clear where it ends?
ssh $user@$host "$command" &
Another gotcha is if the keys are not exchanged ssh stops in an interative mode asking for passwords - really silly programming IMHO - so your script would hang there indefinitely prompting perhaps cron or so for an answer.