Exit the script when all servers are down


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exit the script when all servers are down
# 1  
Old 09-25-2013
Exit the script when all servers are down

Hello All,

I have a script which needs to exit once it encounters that all the servers are down. In the below portion of a code the content manager is having values of server for example server A , server B. So I want the script to exit only if all the servers are down and to continue if one any one of them got failed but the other is fine.


Code:
for server in $content_manager
        do
        ssh cognos@"$server"m 'ls && exit'
        done
        if (($? > 0))
        then
       echo "Content Manager server is Down"
       exit
        fi

# 2  
Old 09-25-2013
Code:
cnt=0;
while [ $cnt -eq 0 ]
do
    for server in $content_manager
    do
        ping "$server"m || cnt=$(( $cnt + 1 ))   # I assume "$server"m resolves to a valid host name.....
   done
   if [ $cnt -eq 0 ] ; then        
       echo "Content Manager server is Down"
       exit
    fi
    sleep 1  # do not busy wait.
    cnt=0   # start counting over again
done

# 3  
Old 09-25-2013
Thanks for the Reply Jim.

The problem is that the ping command is not working properly on my system (redhat) It get stuck on the command prompt. That's why I am trying to check the status of server using ssh command. Is there any other alternative of ping.
# 4  
Old 09-25-2013
I'm wondering why you can't execute the ping command in the first place. Maybe it has been excluded from your $PATH and you're not root.
Anyway, check if this link or this one shed any light.
Alternatively, I believe the best way to check the status of a whole bunch of servers would be setting up a NMS such as Zabbix or Nagios.
Hope it helps.
# 5  
Old 09-27-2013
This contains the positive and negative logic
Code:
#endless loop
while :
do
  alive=0
  dead=0
  for server in $content_manager
  do
    uptime=`ssh -qnx cognos@"$server"m 'uptime' 2>/dev/null`
    if [ $? -eq 0 ]
    then
      alive=$(( $alive + 1 ))
      echo "$server : $uptime"
    else
      dead=$(( $dead + 1 ))
    fi
    sleep 1 # give the OS time to release sockets
  done
  if [ $alive -eq 0 ]
  then
    echo "Content Manager server is Down"
    exit
  fi
  if [ $dead -gt 0 ]
  then
    echo "Content Manager server is degraded"
  fi
done

--
In RedHat you would need ping -c 1 to stop after one sample.
But testing a service like ssh is safer than ping, and can even deliver something useful.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Exit from script

I am invoking java program from shell script. Is there way to catch internal error and exit from script. though it is logging exceptions and errors script continues. (2 Replies)
Discussion started by: sushma123
2 Replies

2. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

4. Shell Programming and Scripting

How to get the exit status of a command in nner script to the outer script?

Hi all, I have a shell script inside which i am executing another shell script. In the inner script im executing a command. i want the status of that command in the outer script to perform some validations. How to get its status please help!!1 Im using ksh. (2 Replies)
Discussion started by: Jayaraman
2 Replies

5. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

6. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

7. UNIX for Dummies Questions & Answers

trying to grep the first few lines of a continuos script, and exit the script anyidea

Hi. I am trying to extract the output of the first few lines of a continuos sh script. The when i run it, i wont to grep the the first 20 lines of it for an entry and basically do a crtl z out of it or to that effect, and output the results to a text file. I basically want to script... (5 Replies)
Discussion started by: k00061804
5 Replies

8. UNIX for Dummies Questions & Answers

Exit out of the Script Command inside a Script

I'm new to Linux. I have a bash script that invokes an executable. I'd like use the SCRIPT command inside the script and exit out of the script command after it writes to the file. Does this make sense? Below is an example of the contents of my script. #BEGIN SCRIPT script typescript... (6 Replies)
Discussion started by: jmungai
6 Replies

9. Shell Programming and Scripting

exit from script

I have a shell script with options, one of which should exit the system (logout), however when I select this option it drops down to shell, is there a command other than exit that will close the session completely ? (1 Reply)
Discussion started by: gefa
1 Replies

10. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies
Login or Register to Ask a Question