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.
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.