![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Execute db2 commands in unix | rollthecoin | AIX | 3 | 04-25-2008 10:17 PM |
| lom don't execute commands | pasalagua | SUN Solaris | 6 | 01-25-2008 01:22 PM |
| How to execute multiple commands via ssh | srage | Shell Programming and Scripting | 9 | 01-05-2008 12:18 AM |
| Execute Loop in Telnet | Dastard | Shell Programming and Scripting | 2 | 07-24-2007 08:38 AM |
| how to execute a while loop for 20 minutes? | subhotech | UNIX for Dummies Questions & Answers | 6 | 07-04-2007 11:54 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Execute commands parallel in a for loop ?
Hi,
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 ? thanks for your help again... |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
try this,
with every instance executing command on 10 servers ... not tested Code:
>subscript.sh #!/bin/bash file=$1 for server in `cat $file` ; do ssh admin@$server "command to run" done /bin/rm $1 exit 0 Code:
>mainscript.sh #!/bin/bash file="/tmp/serverlist" TMPFILE="/tmp/" cnt=0 filecnt=1 headcnt=10 tailcnt=`cat $file | wc -l` while [ $cnt -lt $tailcnt ] do head -$headcnt $file | tail -10 > $TMPFILE$filecnt subscript.sh $TMPFILE$filecnt & cnt=$(($cnt + 10) headcnt=$(($headcnt + 10)) filecnt=$(($filecnt + 1)) done exit 0 |
|
#3
|
|||
|
|||
|
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?
Code:
ssh $user@$host "$command" & bakunin Last edited by bakunin; 11-25-2005 at 02:02 PM. |
|
#4
|
||||
|
||||
|
put the ssh command in a sub-shell in the background ...
Code:
#!/bin/bash file="/tmp/serverlist" for server in `cat $file` ; do (ssh admin@$server "command to run" < /dev/null &) done |
||||
| Google The UNIX and Linux Forums |