Advertisement
Guest User

shutdownscript.sh

a guest
May 12th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.45 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. check_online()
  4. {
  5.    myHost=$1
  6.    pingcount=`ping -c 4 $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }'`
  7.    if [ "$pingcount" -eq 0 ]; then
  8.       # 100% failed
  9.       return 0
  10.    else
  11.       return 1
  12.    fi
  13. }
  14.  
  15. wait_for_offline()
  16. {
  17. timeout=$2
  18. timeoutcount=0
  19.  
  20. check_online "$1"
  21. onlinenow="$?"
  22.  
  23. while [ $onlinenow -eq 1 -a $timeoutcount -lt $timeout ]; do
  24.    echo "$1 : $timeoutcount seconds and still online, waiting for offline..."
  25.    timeoutcount=`expr $timeoutcount + 15`
  26.    sleep 15
  27.    check_online "$1"
  28.    onlinenow="$?"
  29. done
  30.  
  31. if [ $onlinenow -eq 0 ]; then
  32.    echo "$1 : is offline after $timeoutcount seconds"
  33.    return 1
  34. else
  35.    echo "$1 : timeout on going offline after $timeoutcount seconds"
  36.    return 0
  37. fi
  38.  
  39. }
  40.  
  41. shutdown_server()
  42. {
  43.    check_online "$1"
  44.    online="$?"
  45.  
  46.    if [ $online -eq 1 ]; then
  47.       echo "$1 : detected online, need to shutdown"
  48.       echo "Executing: shutdown"
  49.  
  50.       ssh root@$1 'shutdown -h now'
  51.  
  52.       wait_for_offline $1 120
  53.  
  54.       offline="$?"
  55.       if [ $offline -eq 1 ]; then
  56.          echo "$1 : Successfully shutdown at $(date)"
  57.          return 0
  58.       else
  59.          echo "$1 : Timeout waiting to shutdown at $(date)"
  60.          return 1
  61.       fi
  62.    else
  63.       echo "$1 : Not online, no need to shutdown."
  64.       return 0
  65.    fi
  66. }
  67.  
  68. echo "Starting shutdown process at $(date)"
  69. shutdown_server "farm"
  70. exit 0 #ignore failures at this point, just exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement