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.

for server in $content_manager
        do
        ssh cognos@"$server"m 'ls && exit'
        done
        if (($? > 0))
        then
       echo "Content Manager server is Down"
       exit
        fi
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

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.

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.

This contains the positive and negative logic

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