在cron作业中放置时,无法识别shell中存在的命令

时间:2014-03-25 12:34:26

标签: bash unix cron ping mailx

我有一个监控服务器的脚本,如果服务器不能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

1 个答案:

答案 0 :(得分:1)

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

export PATH=/bin:/usr/bin

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