bash 用于检查服务器是否在线的 Ping 工具
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18258364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ping Tool to check if server is online
提问by MasterGberry
Is this tool that I created from various SOF threads valid? Will it work? I want to have a ping test done to a server every minute. If it fails 5 times in a row it sends an email out. It then flushes and resets the script pretty much to check again.
我从各种 SOF 线程创建的这个工具有效吗?它会起作用吗?我想每分钟对服务器进行一次 ping 测试。如果连续 5 次失败,它会发送一封电子邮件。然后它会刷新并重置脚本以再次检查。
#!/bin/bash
# ping checker tool
numOfFails=0
incrememnt=1
EMAILMESSAGE="/tmp/emailmessage.txt"
while true; do
if ! ping -c 1 google.com ; then #if ping exits nonzero...
numOfFails=$(($num + $increment))
else
numOfFails=0
fi
if ((numOfFails > 4)); then
numOfFails=0
echo "SAN is offline!" > $EMAILMESSAGE
mail -s "SAN offline" "[email protected]" < $EMAILMESSAGE
fi
sleep 60 #check again in one minute
done
回答by Oli
Your code won't work at all, this is a revised version:
您的代码根本不起作用,这是一个修订版:
#!/bin/bash
# ping checker tool
FAILS=0
EMAIL_ADDRESS="[email protected]"
SERVER="192.168.1.1"
SLEEP=60
while true; do
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
FAILS=$[FAILS + 1]
else
FAILS=0
fi
if [ $FAILS -gt 4 ]; then
FAILS=0
echo "Server $SERVER is offline!" \
| mail -s "Server offline" "$EMAIL_ADDRESS"
fi
sleep $SLEEP #check again in SLEEP seconds
done
Change [email protected]
and 192.168.1.1
for your email address and the IP address of the server you are testing. I recommend using and IP address instead of a hostname to prevent mixing name resolution errors with connection errors.
更改[email protected]
和192.168.1.1
您的电子邮件地址和要测试的服务器的IP地址。我建议使用 IP 地址而不是主机名,以防止将名称解析错误与连接错误混合在一起。
Please be advised that although this will work I would recommend running a slightly different script from cron instead of having it running continuously like you seem to want, when running from cron you would not need to monitor that the script is running since if it stops for some reason the monitoring of the server stops as well.
请注意,虽然这会起作用,但我建议运行与 cron 略有不同的脚本,而不是让它像您想要的那样连续运行,当从 cron 运行时,您不需要监视脚本是否正在运行,因为如果它停止运行出于某种原因,服务器的监控也停止了。
Something like this run from crontab every minute.
每分钟都会从 crontab 运行这样的东西。
#!/bin/bash
# ping checker tool
TMP_FILE="/tmp/ping_checker_tool.tmp"
if [ -r $TMP_FILE ]; then
FAILS=`cat $TMP_FILE`
else
FAILS=0
fi
EMAIL_ADDRESS="[email protected]"
SERVER="192.168.1.1"
ping -c 1 $SERVER >/dev/null 2>&1
if [ $? -ne 0 ] ; then #if ping exits nonzero...
FAILS=$[FAILS + 1]
else
FAILS=0
fi
if [ $FAILS -gt 4 ]; then
FAILS=0
echo "Server $SERVER is offline!" \
| mail -s "Server offline" "$EMAIL_ADDRESS"
fi
echo $FAILS > $TMP_FILE
回答by dlink
Consider using Pingdom. It provides this service for you.
考虑使用 Pingdom。它为您提供这项服务。
One thing you have not considered is once your site goes down, you will continue to get email messages every minute, until the site is up again, or until you stop this script.
您没有考虑过的一件事是,一旦您的网站出现故障,您将继续每分钟收到电子邮件,直到网站再次启动,或者直到您停止此脚本。
A good approach is to switch states from reporting when the site is down to reporting when the site is up, once you have detected that it is down. And then back again, once it is back up.
一个好的方法是在检测到站点关闭后,将状态从站点关闭时的报告切换到站点启动时的报告。然后再回来,一旦它备份。
Essentially you one receive an email reporting 'site down', then another later on, hopefully, reporting 'site is up'.
基本上,您会收到一封电子邮件,报告“站点关闭”,然后又收到一封电子邮件,希望之后会报告“站点已启动”。
Pingdom does this for you, very nicely.
Pingdom 为您做了这件事,非常好。
回答by kabadisha
I have been investigating how to do this so that I can activate/deactivate services depending on whether my phone is at home. I have come up with the following:
我一直在研究如何做到这一点,以便我可以根据我的手机是否在家里来激活/停用服务。我想出了以下几点:
#!/bin/bash
HOST_TO_CHECK=<hostname>
if ping -qc 20 $HOST_TO_CHECK >/dev/null; then
echo "Host $HOST_TO_CHECK is up"
else
echo "Host $HOST_TO_CHECK is down"
fi
Replace <hostname>with the host you wish to check.
将<hostname>替换为您要检查的主机。
The script will ping the host 20 times. The reason it does this is that my mobile doesn't always respond to pings immediately.
该脚本将 ping 主机 20 次。这样做的原因是我的手机并不总是立即响应 ping。
Obviously you can replace the echo commands with something to actually do something useful :-)
显然你可以用一些东西来代替 echo 命令来做一些有用的事情:-)
You can then schedule the script to check every 5 minutes by adding it to your crontab:
然后,您可以通过将脚本添加到您的 crontab 来安排脚本每 5 分钟检查一次:
*/5 * * * * /opt/pingcheck.sh