サーバーを監視するスクリプトがあります。サーバーが 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