16

I have added a cron job for my incremental backup, and I configured it like this:

0 23 * * * /usr/bin/rsync -ravzX /mnt/external/project/ /media/backup/project/ | mail -s "Backup Success" admin@example.com

But i didn't receive any e-mails.
How can I find out what's wrong?

10
  • 1
    Check mail.log entry
    – masegaloeh
    May 11, 2015 at 6:41
  • 2
    Can you send mail successfully using mail from the command line? May 11, 2015 at 6:51
  • @paul: its working in mail command
    – Booth
    May 11, 2015 at 7:01
  • @masegaloeh: no log entry
    – Booth
    May 11, 2015 at 7:03
  • 3
    Check your cron log.
    – Jenny D
    May 11, 2015 at 7:26

3 Answers 3

34

You can use the MAILTO option in crontab to define your email address and receive all output and errors in all crons running.

open crontab using

crontab -e

on the top of the file use MAILTO option as

MAILTO=email@example.com

cron looks for MAILTO feature to decide where it should send cron logs. it send is to root by default if the crons are running with root.

put it there on the top and remove any mail command reference from the crons.

Test and verify if you receive cron alerts after this.

3
  • My backup mail is in spam folder.Why it goes to spam?how to move it on inbox?
    – Booth
    May 11, 2015 at 12:28
  • 1
    The email delivery is something you have to consider as per your requirements. If you are the only one who receives such email then simply mark that email as "Not Spam" and it will land in inbox next time. But if you are looking for a generic email solution that will deliver email in inbox then i recommend you must read the best practices like Setup a Reverse DNS record for your Server IP address, a PTR record , an SPF record that permits legitimate servers an outgoing email on behalf of that domain etc.. May 11, 2015 at 12:32
  • Can you also set REPLYTO? Or FROM? Didn't seem to work for me.
    – Houman
    Mar 16, 2020 at 18:59
3

On FreeBSD

1: Check log:

tail -f /var/log/cron
tail -f /var/log/maillog

2: Replace sendmail with ssmtp vi /etc/rc.conf file.. and add:

sendmail_enable="NO"
sendmail_submit_enable="NO"
sendmail_outbound_enable="NO"
sendmail_msp_queue_enable="NO"

3: Launch following commands:

killall sendmail
cd /usr/ports/mail/ssmtp/
make install replace clean

4: Edit ssmtp conf file:

vi /usr/local/etc/ssmtp/ssmtp.conf

.... and add following lines:

root=yourrealemail@example.com
mailhub=smtp.example.com:465
RewriteDomain=example.org
UseTLS=YES
AuthUser=user@example.com
AuthPass=password222
FromLineOverride=YES
Hostname=yourhostname

5: Enter following command:

echo ‘ssmtp_enable=“YES”’ >> /etc/rc.conf

p.s. other explained options of ssmtp.conf are here:

http://www.techrepublic.com/blog/it-security/use-ssmtp-to-send-e-mail-simply-and-securely/

2
  • 16
    This would have been a great answer if the question had been "How do I get SSMTP to send mail via gmail on FreeBSD". Unfortunately, the question is about cron & mail on Linux.
    – Jenny D
    May 11, 2015 at 7:27
  • What exactly was the OP's question: "How can I send an email after a cron job?", or "How can I find out what's wrong (with my mail setup)?"
    – papiro
    Dec 10, 2016 at 16:35
3

Problem in this case was that message had been delivered to spam folder on gmail (which is always worth checking). Adding sender as a contact should avoid this.

Apart from that, looking at the received message's full headers may give clues as to why it was flagged as spam (and so how to influence filtering not to do this).

Not the answer you're looking for? Browse other questions tagged or ask your own question.