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