我有一个脚本来监控我的服务器,如果服务器无法 ping,它会发送邮件警报。但是当我将脚本设置为 cron 作业时,它会抛出错误,因为 ping 命令无法识别,mailx 命令无法识别;而手动执行时同样有效。
下面是脚本的代码
#!/bin/sh
cd `dirname $0`
serverIPs="192.0.0.40 192.0.0.140"
count=4
##checking the status by pinging the individual ips in serverIps variable
for host in $serverIPs
do
recCount=$(ping -c $count $host | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $recCount -eq 0 ]; then
# 100% failed
echo "Host : $host is down (ping failed) at $(date)" |mailx -s "Server is not responding completely " jagdeep.gupta@gmail.com
elif [ $recCount -lt 4 ]
then
echo "Host : $host is not responding well there is loss of packets , please check " |mailx -s "Server is not responding partially " jagdeep.gupta@gmail.com
fi
done