I have a script to monitor my server, it sends a mail alert if the server are not ping-able. But when I set the script as a cron job , it throws error as ping command not recognized , mailx command not recognized ; while the same is working when executed manually.
Below is the code of the script
#!/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
Your Cron Daemon likely flushes the environment, along with the $PATH
variable. Try to add
export PATH=/bin:/usr/bin
at the beginning of your script. (This should suffice. If it does not, check the output of echo $PATH
and use that as the value.)