bash - Ping Tool to check if server is online -
is tool created various sof threads valid? work? want have ping test done server every minute. if fails 5 times in row sends email out. flushes , resets script pretty check again.
#!/bin/bash # ping checker tool numoffails=0 incrememnt=1 emailmessage="/tmp/emailmessage.txt" while true; if ! ping -c 1 google.com ; #if ping exits nonzero... numoffails=$(($num + $increment)) else numoffails=0 fi if ((numoffails > 4)); numoffails=0 echo "san offline!" > $emailmessage mail -s "san offline" "test@test.com" < $emailmessage fi sleep 60 #check again in 1 minute done
your code won't work @ all, revised version:
#!/bin/bash # ping checker tool fails=0 email_address="example@example.com" server="192.168.1.1" sleep=60 while true; ping -c 1 $server >/dev/null 2>&1 if [ $? -ne 0 ] ; #if ping exits nonzero... fails=$[fails + 1] else fails=0 fi if [ $fails -gt 4 ]; fails=0 echo "server $server offline!" \ | mail -s "server offline" "$email_address" fi sleep $sleep #check again in sleep seconds done
change example@example.com
, 192.168.1.1
email address , ip address of server testing. recommend using , ip address instead of hostname prevent mixing name resolution errors connection errors.
please advised although work recommend running different script cron instead of having running continuously seem want, when running cron not need monitor script running since if stops reason monitoring of server stops well.
something run crontab every minute.
#!/bin/bash # ping checker tool tmp_file="/tmp/ping_checker_tool.tmp" if [ -r $tmp_file ]; fails=`cat $tmp_file` else fails=0 fi email_address="example@example.com" server="192.168.1.1" ping -c 1 $server >/dev/null 2>&1 if [ $? -ne 0 ] ; #if ping exits nonzero... fails=$[fails + 1] else fails=0 fi if [ $fails -gt 4 ]; fails=0 echo "server $server offline!" \ | mail -s "server offline" "$email_address" fi echo $fails > $tmp_file
Comments
Post a Comment