0

我有一个脚本来监控我的服务器,如果服务器无法 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
4

1 回答 1

1

您的 Cron 守护程序可能会连同$PATH变量一起刷新环境。尝试添加

export PATH=/bin:/usr/bin

在脚本的开头。(这应该足够了。如果没有,请检查输出echo $PATH并将其用作值。)

于 2014-03-25T12:37:34.937 回答