Advertisement
Uno-Dan

Wait for reboot update

Jun 18th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.23 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. format_time() {
  7.     seconds=$1
  8.     time=`printf "%02d-%02d-%02d\n" $[seconds/3600] $[$[seconds%3600]/$[60]] $[$[seconds%3600]%$[60]]`
  9.     echo $time
  10. }
  11.  
  12. wait_for_reboot() {
  13.     cnt=0
  14.     host=$1
  15.     start=`date +%s`
  16.    
  17.     ping -c 1 -n -w 1 $host | grep "1 received" > /dev/null
  18.     [ $? != 0 ] && status=down || status=up
  19.     [ "$2" == "" ] && timeout=90 || timeout=$2
  20.    
  21.     echo
  22.     echo -n "Waiting for host \"$host\" to come online: "
  23.    
  24.     while true; do
  25.         ping -c 1 -n -w 1 $host | grep "0 received" > /dev/null
  26.         result=$?
  27.        
  28.         echo -n "."
  29.         cnt=$((cnt+1))
  30.         [ $result == 1 ] && sleep 1 || status=down
  31.        
  32.         [ $result == 1 ] && [ $status == "down" ] && {
  33.             sleep 1
  34.             end=`date +%s`
  35.             echo -e "\nHost \"$host\" is back online, reboot time: $(format_time $[end-start])"
  36.             break
  37.         } || [ $cnt -ge $timeout ] && {
  38.             end=`date +%s`
  39.             echo -e "\nRemote host \"$host\" did not reboot. [timeout > $timeout]\n"
  40.             break
  41.         }
  42.     done
  43. }
  44.  
  45. echo
  46. ssh root@$HOST reboot
  47.  
  48. wait_for_reboot $HOST $TIMEOUT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement