Sunday, January 1, 2017

Cron output not working



Need help figuring this one out.
I have the following script - /root/eximqueue.sh with the proper +x rights, etc:



#!/bin/bash
######### Edit here ##########
_mail_user=my@address.co.za # Set this to your email id to receive alerts on mail queue
_limit=20 # Set the limit here


##############################

clear;
_result="/tmp/eximqueue.txt"
_queue="`exim -bpc`"

if [ "$_queue" -ge "$_limit" ]; then
echo "Current queue is: $_queue" > $_result
echo "Summary of Mail queue" >> $_result

echo "`exim -bp | exiqsumm`" >> $_result
mail -s "Number of mails on `hostname` : $_queue" $_mail_user < $_result
cat $_result
_message_id="`exiqgrep -i -f my@address.co.za | xargs exim -M`"
fi

rm -f $_result


I then setup the cron, checked my crons (crontab -l) and its there:




*/5 * * * * /bin/sh /root/eximqueue.sh


Checked my cron logs



grep eximqueue /var/log/cron


...and its running (just a few for examples sake):




Oct 12 14:00:01 osi CROND[28191]: (root) CMD (/bin/sh /root/eximqueue.sh)
Oct 12 14:05:01 osi CROND[30877]: (root) CMD (/bin/sh /root/eximqueue.sh)
Oct 12 14:10:01 osi CROND[893]: (root) CMD (/bin/sh /root/eximqueue.sh)
Oct 12 14:15:01 osi CROND[4429]: (root) CMD (/bin/sh /root/eximqueue.sh)


Problem is, I'm not getting any email from the script! However, if I run it directly - it works perfectly and I get the email.
Ideas?


Answer




The problem is that when a script starts from cron, the PATH is much shorter than when you run it from bash directly.



Just replace the calls to the program to the full path to them. /usr/sbin/exim instead of exim for example (or where you have installed exim).


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...