cron table mailx caused empty content

1

I'm using centos 7.0/6.5. The content of /etc/cron.hourly/mail.cron

#!/bin/sh
log=/var/log/mail.cron.log
echo "`hostname` `date`" >> $log 2>&1
mailx -s "test mail" me@example.com < $log

The result of /var/log/mail.cron.log

myhost Mon Jul 28 11:01:01 CST 2014

But, I got a empty e-mail, with correct subject. So, I test this cron manually. I got the correct e-mail content.

myhost Mon Jul 28 11:01:01 CST 2014
myhost Mon Jul 28 11:30:29 CST 2014

Here is the related log /var/log/cron

Jul 28 11:01:01 myhost run-parts(/etc/cron.hourly)[24577]: starting mail.cron
Jul 28 11:01:02 myhost run-parts(/etc/cron.hourly)[24605]: finished mail.cron

I'm wonder how to debug this problem.

Daniel YC Lin

Posted 2014-07-28T03:34:36.977

Reputation: 795

What is the output of crontab -l?. Also, try changing echo "\hostname` `date`" >> $log 2>&1toecho "$(hostname date)" >> $log`. – jimm-cl – 2014-07-28T04:50:28.197

@jim, 1. We don't require crontab -l to check cron. I'm sure the cron job works, I got the e-mail. The problem is the email's content is empty, it should contains the log. I don't require change to $(hostname date), that is not the problem. – Daniel YC Lin – 2014-07-28T06:22:46.300

Answers

2

A few things to try.

  1. !/bin/sh -xv

  2. Try redirecting stderr with two greater than signs, 2>>&1 .

The first will give you step-by-step output of your script. I'd try it first without redirecting stderr to your log file. Cron should mail you a copy of the output. You can see exactly where it goes wrong.

The second. Maybe you have an error that's clobbering your stdout? (Probably not, it works as you have it in CentOS 6.5.)

You could also check for unprintable characters using cat.

cat -vet /etc/cron.hourly/mail.cron

Still another thing to try is to use absolute paths to your commands, /bin/echo instead of echo, /bin/date instead of date, /bin/hostname instead of hostname. (ymmv) Jim is absolutely correct in cron doesn't always use the exact environment as the same interactive user.

IAmJeff

Posted 2014-07-28T03:34:36.977

Reputation: 142

1If I run the /etc/cron.hourly/mail.cron directly in root shell. It works. So, I think the problem is not caused by the redirection. – Daniel YC Lin – 2014-07-29T01:23:33.527

Just test this on CentOS 6.5, it occurs problem too. You could try it. – Daniel YC Lin – 2014-07-29T02:14:39.117

My test on CentOS 6.5 as root worked. I used /var/tmp instead of /var/log, though. Did you try with the -xv flags? That will print the commands and values the script uses as it runs. I frequently debug shell scripts this way. – IAmJeff – 2014-07-30T13:55:41.423

1

You are sure the cron job works, as per your comments - however, you have tested it manually running the job while you are logged-in. The thing is that the jobs ran by cron do not take into account the settings and environment variables you have with your current user, so you probably need to source the .bash_profile settings file for the user as a step prior to sending the email in your script.

Change your script to this:

#!/bin/sh
source $HOME/.bash_profile     # or whatever profile file or setting you are using
log=/var/log/mail.cron.log
echo "$(hostname) $(date)" >> $log
mailx -s "test mail" me@example.com < $log

cron knows nothing about your shell - it is started by the system, so it has a minimal environment. If you want it to be able to find your programs or run utilities installed in your system, you have to let it know where those are in the first place, and you can do that by setting the .profile variables at the beginning of your script.

Some useful links:

jimm-cl

Posted 2014-07-28T03:34:36.977

Reputation: 1 469

I've tested. This is not work. Even include $HOME/.bash_profile. – Daniel YC Lin – 2014-07-29T10:00:54.510

0

I saw that you got some answers but not a clear working answer. What worked for me was to set the cron this way:

*/5 * * * * /fullpath/cron.sh | ( body="$(cat)"; if [[ -n $body ]]; then echo "$body" | mail -s "Cron script output" me@example.com; fi)

this captures the output and sends it to mail without it I get an empty email even when running in a script. (thanks to colucix)

Jinxmcg

Posted 2014-07-28T03:34:36.977

Reputation: 101