cron not sending external mails

This script is to send a email if the IP adress is active
when i run this script it is working and sending emails to external users in different domains, but when this script is scheduled in a cron it is unable to send the emails

 
#/bin/sh
ifconfig -a | grep 192.168.0.1
if [ $? == '0' ];
then mail -s "subject" mail.domain.com < /home/user/mailmessage.txt
fi
 

So i used a while loop avoding cron, Below is the code, This is able to send the emails,

#/bin/sh
while :
do
ifconfig -a | grep 3.229.224.157
if [ $? == '0' ]
then 
   mail -s "CRITICAL!! PAN Credit Application Load Balancing Has Failed Over to MYUKSLCAP2LB002" -c \
  emailaddress@fqdn.com < \
   /home/smg_prod/mailmessage.txt
fi
sleep 60
done

But for the same code why it is not able to send emails when set via cron, checked cronlogs and found no errors, cron is executed as schedules, also checked messages no errors found

Go with numerical comparator ..

if [ $? -eq 0 ];

Check the hash-bang statement in the first line, it should be #!/bin/sh .
Does the user have privileges for running cron? Paste the cron entry.

--ahamed

Did you check if it works when being run manually from a shell?
There is also missing a ! in the shebang:

#!/bin/ksh
...

Yes running as root

Tried changing the first line but still not working

Paste the cron entry... crontab -l

--ahamed

i tried changing the code

#!/bin/sh

but cron is not sending the emails but when run manually from shell it is sending the mails i checked now

---------- Post updated at 09:29 AM ---------- Previous update was at 09:28 AM ----------

26 * * * * /home/user/Loadbalancercheck.sh

particular entry for the script

Try executing the script with -x thru cron

Script changes...

#!/bin/sh
set -x
ifconfig...

Cron entry...

26 * * * * /home/user/Loadbalancercheck.sh >/tmp/log 2>&1

Paste the output of /tmp/log

--ahamed

Mail log entry for the cronmail can some one pelase explain this

Dec  6 09:23:00 Hostname sendmail[17484]: pB69N0017484: from=root, size=288, class=0, nrcpts=1, msgid=<201112060923.pB69N0017484@hostname.domain.com>, relay=root@localhost
Dec  6 09:23:00 Hostname sendmail[17484]: pB69N0017484: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=local, pri=30288, dsn=2.0.0, stat=Sent
Dec  6 09:23:00 Hostname2 sendmail[17484]: pB69N0017484: to=root, ctladdr=root (0/0), delay=00:00:00, xdelay=00:00:00, mailer=local, pri=30288, dsn=2.0.0, stat=Sent

Starting a script through cron is not the same as when being started from a shell manually. You should ensure that all binaries etc. are found since you have not the same environment as in your shell. PATH variable and others are not set if you do not set them explicitly.

Here is a general guide to cron with things noted you should take into account:

i tried the above, log entry for the cron

[root@Hostname]# cat /tmp/log22
+ ifconfig -a
/home/user/Loadbalancercheck1.sh: ifconfig: command not found
+ grep 192.168.0.1
+ '[' 1 -eq 0 ']'

---------- Post updated at 09:45 AM ---------- Previous update was at 09:40 AM ----------

Thanks a lot realy valuable

---------- Post updated at 09:48 AM ---------- Previous update was at 09:45 AM ----------

#!/bin/sh
/sbin/ifconfig -a | grep 192.168.0.1
if [ $? -eq '0' ];
then mail -s "subject" user@Domain.com < /home/user/mailmessage.txt
fi

Resolved

code changed

Really helpfull friends
Thank you evry one for your time :slight_smile: