#!/bin/bash ## Author Luis@32768.org at 2012-02-14 ## check ping for ups every 2 mins, if down, shutdown in 3 mins, if ok within 2 mins, recover shutdown ## while true ## do ## get the gateway IP GWIP='127.0.0.1' let n=0 for x in `/sbin/route | grep default` do if [ $n -eq 1 ] then GWIP=$x break fi let n=n+1 done ping -c 1 $GWIP > /dev/null ret=$? if [ $ret -eq 0 ] then # $ret = 0 # router is OK #echo "Router is UP!" ## sleep 10 pscnt=`ps | grep halt | grep -v grep | wc -l` if [ $pscnt -gt 0 ] then echo "Luis_UPS: AC Power/Network Recovered! Cancel shutdown!" ##/sbin/shutdown -c "Luis_UPS: AC Power/Network Recovered! Cancel shutdown!" & /usr/bin/killall halt fi else # $ret = 1 # if router fail, it meas 220V is down, or network is down echo "Luis_UPS: AC Power/Network Failed! Ready to shutdown in 180s!" pscnt=`ps | grep halt | grep -v grep | wc -l` if [ $pscnt -eq 0 ] then # if no shutdown in progress, then shutdown echo "Luis_UPS: AC Power/Network Failed! Ready to shutdown in 3 mins!" ##/sbin/shutdown -h 3 "Luis_UPS: AC Power/Network Failed! Ready to shutdown!" & /sbin/halt -d 180 & fi fi ## done