Advertisement
Uno-Dan

Wait for reboot

Jun 18th, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.94 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. HOST=$1 # Resolvable host name or IP address of remote host.
  4. TIMEOUT=$2
  5.  
  6. wait_for_reboot() {
  7.     cnt=0
  8.     host=$1
  9.    
  10.     ping -c 1 -n -w 1 $host | grep "1 received" > /dev/null
  11.     [ $? != 0 ] && status=down || status=up
  12.     [ "$2" == "" ] && timeout=90 || timeout=$2
  13.    
  14.     echo
  15.     echo -n "Waiting for host \"$host\" to come online: "
  16.    
  17.     while true; do
  18.         ping -c 1 -n -w 1 $host | grep "0 received" > /dev/null
  19.         result=$?
  20.        
  21.         echo -n "."
  22.         cnt=$((cnt+1))
  23.         [ $result == 1 ] && sleep 1 || status=down
  24.        
  25.         [ $result == 1 ] && [ $status == "down" ] && {
  26.             sleep 1
  27.             echo -e "\nHost \"$host\" is back online."
  28.             break
  29.         } || [ $cnt -ge $timeout ] && {
  30.             echo -e "\nRemote host \"$host\" did not reboot.\n"
  31.             break
  32.         }
  33.     done
  34. }
  35.  
  36. echo
  37. ssh root@$HOST reboot
  38.  
  39. wait_for_reboot $HOST $TIMEOUT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement