BlogBash Restart on Network Failure

Bash Restart on Network Failure

Posted by Jesse on December 13, 2010

[UPDATED INFORMATION]

Here's a bash script I'm using on my server to restart the server in the event network goes down.  I set this up because during a large file transfer through Samba, the server's network stopped responding.  To be on the safe side, i'm doing a full restart.

The scripts attempts to ping HOST (192.168.1.1, my router), MAX_FAILURES (3) times.  After the 3rd failure, the server will restart.  If the network comes back before the restart, we'll proceed as if nothin happened.  I'm waiting 5 minutes between each attempt.

I'm running this through an hourly cronjob.

  1. #!/bin/bash
  2. ####################################################################
  3. # Shell script for monitoring ping to a certain host, and if the #
  4. # ping fails max_failures consecutive times, restart the machine. #
  5. # #
  6. # The main driver behind this script is that my server is located #
  7. # remotely and if something happens to kill the network adapter #
  8. # but not the whole system, I'd like the system to reboot to the #
  9. # nominal state so I can regain control. #
  10. ####################################################################
  11. # Recommend calling this in a cronjob at a frequency less than #
  12. # MAX_FAILURES * SLEEP_TIME. #
  13. ####################################################################
  14.  
  15. ### EDIT THESE ###
  16. HOST="192.168.1.1"
  17. MAX_FAILURES=3
  18. SLEEP_TIME=3000 #3000 seconds = 5 minutes
  19. ### END EDIT ###
  20.  
  21. # Put a timestamp infront of every echo
  22. function echo {
  23. /bin/echo netfaildetect: `date`: $*
  24. }
  25.  
  26. cur_failures=0
  27.  
  28. while [ $cur_failures -lt $MAX_FAILURES ]; do
  29. ping -c 1 $HOST > /dev/null
  30.  
  31. #Try pinging, and if host is up, we bail
  32. if [ $? -eq 0 ]; then
  33. cur_failures=0
  34. break
  35. fi
  36. let cur_failures=cur_failures+1
  37. echo "Network failure to $HOST detected. Attempt:$cur_failures/$MAX_FAILURES."
  38.  
  39. if [ $cur_failures -lt $MAX_FAILURES ]; then
  40. sleep $SLEEP_TIME
  41. fi
  42. done
  43.  
  44. if [ $cur_failures -ge $MAX_FAILURES ]; then
  45. echo "Too many failures occured, restarting"
  46. reboot
  47. fi
  48.  
  49. echo "Network is nominal"

Download Script

Comments:

Posted by ltdeafybdp on
FrGP4N thvnajbbsdxu, [url=http://ycjqwccjoprh.com/]ycjqwccjoprh[/url], [link=http://gleyflwiosmx.com/]gleyflwiosmx[/link], http://ttyjjwgvtgtp.com/
Posted by Nikos on
Nice script... gave me a quick solution for a similar problem.
A couple of typos though:
3000 seconds = 50 minutes
infront is "in front"
"Network is nominal" should probably be "Network is normal"
Leave a Reply



(Your email will not be publicly displayed.)


Captcha Code

Click the image to see another captcha.