Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

0,30 * * * * root /some/bash/script

The bash script contains:

if [ -s /var/log/test ]; then
 echo "test" | mailx -s "test" -a /var/log/test -r
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
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


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.