Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am having trouble getting cron to email an attachment based on a predefined condition set in the bash script. The following is my /etc/crontab setting:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=account@email.com
MAILFROM=cron@server.com
0,30 * * * * root /some/bash/script

The bash script contains:

#!/bin/bash
PATH=/bin:/sbin:/usr/sbin:/usr/bin:/usr/local/bin
if [ -s /var/log/test ]; then
 echo "test" | mailx -s "test" -a /var/log/test -r cron@server.com account@email.com
fi
exit 0

The problem is that cron sometimes send out an email despite the test file being empty. Sometimes the subject header "test" is overridden by cron with its own subject header. Without setting "MAILFROM", cron will use root despite the setting in mailx.

I am very new to cron, so I am not very sure whether I have done things correctly. Would appreciate if somebody can point out what is causing the problem.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

You need to understand that cron won't just discard output generated by a job. If a cron job generates output that is otherwise not handled (eg, by mailing it to someone, or putting it into a file, or in some other way) this output will be emailed as described in the MAILTO variable. If you don't want this to happen, you need to make sure that no output can fall out of the end of your job; that's why it's very common to see crontab entries that say, eg,

*/5 * * * *     munin /usr/bin/munin-cron >& /dev/null

That terminal >& /dev/null is to tell cron to discard any output that is not otherwise handled by the executable/script being run.

share|improve this answer
    
Thanks, I understand better now. –  Question Overflow Jan 31 '13 at 9:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.