Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

echo "`hostname` `date`" >> $log 2>&1
mailx -s "test mail" < $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.

share|improve this question
What is the output of crontab -l?. Also, try changing echo "`hostname` `date`" >> $log 2>&1 to echo "$(hostname date)" >> $log. – jimm-cl Jul 28 '14 at 4:50
@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 Jul 28 '14 at 6:22

2 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.

share|improve this answer
If 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 Jul 29 '14 at 1:23
Just test this on CentOS 6.5, it occurs problem too. You could try it. – Daniel YC Lin Jul 29 '14 at 2:14
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 Jul 30 '14 at 13:55

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:

source $HOME/.bash_profile     # or whatever profile file or setting you are using
echo "$(hostname) $(date)" >> $log
mailx -s "test mail" < $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:

share|improve this answer
I've tested. This is not work. Even include $HOME/.bash_profile. – Daniel YC Lin Jul 29 '14 at 10:00

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.