shiva
shiva

Reputation: 429

Cron not sending email

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:

[email protected]
*/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.

Upvotes: 6

Views: 15318

Answers (6)

tash
tash

Reputation: 931

After installation of s-nail and sendmail in CentOS 9, I had to restart crond.service to receive cron job's log over email.

(@cyqsimon already put this as a comment above)

Upvotes: 0

Sridhar Sarnobat
Sridhar Sarnobat

Reputation: 25216

/var/log/mail.err

Check this file to see if you have errors:

/var/log/mail.err

Example

error

In my case it was happening on Ubuntu 22 despite installing mailutils and postfix, and not having an error in /var/log/syslog about No MTA installed

$ tail -f /var/log/mail.err

Jan  9 17:41:16 me-System-Product-Name postfix/sendmail[276051]: fatal: open /etc/postfix/main.cf: No such file or directory

Cause

I installed postfix with "No configuration".

Solution

I could change this using sudo dpkg-reconfigure postfix (Credits: https://ubuntuforums.org/showthread.php?t=1519519 )

Upvotes: 0

Ethan Brown
Ethan Brown

Reputation: 703

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.

Upvotes: 3

Sebastian Tur
Sebastian Tur

Reputation: 31

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

Upvotes: 3

transistor
transistor

Reputation: 669

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.

Upvotes: 0

Quentin
Quentin

Reputation: 943098

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.

Upvotes: 4

Related Questions