script hangs when a remote server is down


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting script hangs when a remote server is down
# 1  
Old 11-01-2007
Question 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
# 2  
Old 11-01-2007
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.
# 3  
Old 11-01-2007
Quote:
Originally Posted by porter
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 --

Quote:
ssh 192.168.1.4 " ...commands .."
ssh 192.168.1.5 " ...commands .."
ssh 192.168.1.6 " ...commands .."
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. Smilie

Thanks.
# 4  
Old 11-04-2007
Data

please help !!
# 5  
Old 11-04-2007
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 :

Code:
.... < 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
# 6  
Old 11-04-2007
Bug

Quote:
Originally Posted by bakunin
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 :

Code:
.... < 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 !!

Last edited by vikas027; 11-04-2007 at 09:26 PM.. Reason: forgot something
# 7  
Old 11-06-2007
Bug

Hi all,

Thanks for your co-operation.


I was looking something like this :--


Quote:
ping -c 1 -w 180 192.168.1.4 1>/dev/null 2>/dev/null

This will wait for 3 mins, if the machine is up, it will return '0'.

Check the return state of the above command and run ssh it the return value is '0'.
Thanks to all.

Regards,
Vikas
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Except script to run a local shell script on remote server using root access

local script: cat > first.sh cd /tmp echo $PWD echo `whoami` cd /tmp/123 tar -cvf 789.tar 456 sleep 10 except script: cat > first #!/usr/bin/expect set ip 10.5.15.20 set user "xyz123" set password "123456" set script first.sh spawn sh -c "ssh $user@$ip bash < $script" (1 Reply)
Discussion started by: Aditya Avanth
1 Replies

2. Shell Programming and Scripting

Script connect to remote server, not find files and exit only from remote server, but not from scrip

I have a script, which connecting to remote server and first checks, if the files are there by timestamp. If not I want the script exit without error. Below is a code TARFILE=${NAME}.tar TARGZFILE=${NAME}.tar.gz ssh ${DESTSERVNAME} 'cd /export/home/iciprod/download/let/monthly;... (3 Replies)
Discussion started by: digioleg54
3 Replies

3. Shell Programming and Scripting

Sudo connect to a remote server and execute scripts in remote server

Hello Every one!! I am trying to write a shell script which will connect to a remote server and execute scripts which are at a certain path in the remote server. Before this I am using a sudo command to change the user. The place where I am stuck is, I am able to connect to the... (6 Replies)
Discussion started by: masubram
6 Replies

4. Shell Programming and Scripting

How to Append the output of a script running in remote server to a file in local server?

Hi guys, So i am in server1 and i have to login to server 2, 3,4 and run some script there(logging script) and output its result. What i am doing is running the script in server2 and outputting it to a file in server 2 and then Scp'ing the file to server1. Similarly i am doing this for other... (5 Replies)
Discussion started by: srkmish
5 Replies

5. Solaris

Script to get files from remote server to local server through sftp without prompting for password

Hi, I am trying to automate the process of fetching files from remote server to local server through sftp. I have the username and password for the remote solaris server. But I need to give password manually everytime i run the script. Can anyone help me in automating the script such that it... (3 Replies)
Discussion started by: ssk250
3 Replies

6. Shell Programming and Scripting

Need help on how to exit a script run on a server from a remote server

hi, I am using the below line to run a script from remote server(say server A) to another server(say server B). ssh username@servername ksh script name. The issue is the script logs into server B, executes the script on server B, transfers the file to server A but does not exit from... (4 Replies)
Discussion started by: yohasini
4 Replies

7. Programming

Expect script to run a Shell script on remote server

Hi All, I am using a expect script to run a shell script on remote server, the code is as follows. But the problem is that it executes only first command, and hangs it doesn't run the next commands. spawn ssh $uid@$host expect "password:" send "$password\r" expect "*\r" send... (2 Replies)
Discussion started by: yashwanthsn
2 Replies

8. Shell Programming and Scripting

How to stop a script running in remote server from local script

Hi, I have googled for quite some time and couldn't able to get what exactly I am looking for.. My query is "how to stop a shell script which is running inside a remote server, using a script"??? can any one give some suggestions to sort this out. (1 Reply)
Discussion started by: mannepalli
1 Replies

9. Shell Programming and Scripting

Can a script runned in local server access remote server?

Hi, Im creating a script that is supposed to run commands on remote server using sftp. My script is as below: #!/bin/ksh sftp remote_server mypassword cd /u08/mydir/allfiles mget * .. But this is what I got when I runned the script: Connecting to remote server...... (3 Replies)
Discussion started by: luna_soleil
3 Replies

10. Linux

Script hangs on the unix server

We have a unix script scheduled to execute once in a day, some times it hangs on the server and never performs its operations, we need to manually kill the process and re-start that script, is there any way to have notification when the script hangs on the server. Thanks & Regards, Murthy. (3 Replies)
Discussion started by: Ramana Murthy
3 Replies
Login or Register to Ask a Question