Cron not sending email

Viewed 10595

I have installed PostFix and sendmail both, and then try to set cron for a python script and want to send an email by cron.

My cron schedule like:

MAILTO=test@example.com
*/2 * * * * python3 /var/test.py >> /var/log/test.log 2>&1

Still Cron is not sending any email.

Please help what i need to do more.

4 Answers

Cron will send the STDOUT and STDERR from the script by email.

>> /var/log/test.log 2>&1

… but your script has redirected them both to a file so there isn't any data to send.

Remove the redirect if you want the data to appear in an email instead.

You need to have sendmail installed. on some mimimal raspibian/Pi OS installs it is missing by default

I've been fighting this problem on a new CentOS 8 installation. I found a reply to this blog posting, https://bobcares.com/blog/crontab-not-sending-email/ where the poster suggested reinstalling "cronie"

yum reinstall cronie

and that worked for my situation.

After setting MAILTO, you should run the command newaliases to let the system update settings.

Also, you have to remove the >> /var/log/test.log 2>&1 part, as this will log the output into the logfile, leaving nothing to email.

If you just want to have the error messages in the email, remove just the 2>&1 part.

Related