#!/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 -w 5 -c 1 $GWIP > /dev/null ret=$? if [ $ret -eq 0 ] then # $ret = 0 # router is OK #echo "Router is UP!" ## sleep 10 pscnt=`ps aux | grep shutdown | 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!" & fi else # $ret = 1 # if router fail, it meas 220V is down, or network is down #echo "Router is DOWN!" #echo "Luis_UPS: AC Power/Network Failed! Ready to shutdown in 180s!" pscnt=`ps aux | grep shutdown | 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!" & fi fi ## done