script hangs when a remote server is down

Hi all,

I have made a script which logins to remote servers and fetches some data from it.
Is is working perfectly when all servers are reachable
BUT
my problem is -- if in case a server is down (or not reachable), the script hangs.

Is there some way, that the script just continues to ssh another server when a particular server is not responding say for 30 sec.

I am using ssh to login into servers and have used ssh-keygen command to bypass passwords.

Pls help, just finalizing the script.

Thanks in adv.

Regards,
Vikas

You could run the ssh in parallel with a sleep, where the sleep kills the other process it it's still running when the sleep wakes up.

How to do that, say I am using this code --

And in this 192.168.1.5 is down, my script should wait for around 30 seconds and if there is no response it should move further to ssh 192.168.1.6.

Pls elaborate a bit, I am new to scripting just 4-5 days old. :slight_smile:

Thanks.

please help !!

Do you know how long the commands on the remote machine will approximately take? If so you could run the ssh-command in the background and then use sleep to let pass this time (plus some for contingency). If the command the still runs, you consider it to be hanging and kill it, if it has finished it is ok. You can use the "jobs" built-in ksh command to check the status of the job.

We suppose the commands you want to run remotely will take 20 seconds approximately. We add 10 seconds just to be sure and put the IP adress of the host to be contacted in a variable which is read in a loop :

.... < your script >....
cat list_of_ipadresses | while read ipadress ; do
     ssh  ssh $ipadress "<...commands ..>" &
     sleep 30
     jobs > /dev/null
     if [ $(jobs -p %s 2>/dev/null| wc -l) -ge 1 ] ; then
          print - "an error occurred contacting $ipadress
          kill %1
     else
          print - "success contacting $ipadress"
     fi
done
...

The first "jobs" statement is there to clear the display of all the "done"-messages if backgroup jobs already terminated during the sleep-statement.

bakunin

thank you so much Bakunin, will try this code and let you know the results.
But I am not very sure about the ksh shell.
Thanks a TON !!

Hi all,

Thanks for your co-operation.

I was looking something like this :--

Thanks to all.

Regards,
Vikas