The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-25-2005
Registered User
 

Join Date: Aug 2004
Location: standing in a Sun 25k, Vienna
Posts: 78
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...
Reply With Quote
Forum Sponsor
  #2  
Old 11-25-2005
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
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
Reply With Quote
  #3  
Old 11-25-2005
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
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" &
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.

bakunin

Last edited by bakunin; 11-25-2005 at 02:02 PM.
Reply With Quote
  #4  
Old 11-27-2005
Just Ice's Avatar
Lights on, brain off.
 

Join Date: Mar 2005
Location: in front of my computer
Posts: 629
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0