Ask Your Question
1

How to make cron send emails?

asked 2015-07-29 05:20:53 -0600

wolfv gravatar image

updated 2015-07-30 20:27:10 -0600

I have cron working on a single-user Fedora22, and am trying to make the cron send emails. This is my crontab:

*/10 * * * * /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local
44 00,12 * * * /home/wolfv/backup_script /home/wolfv gs://wolfv

cron runs the script as scheduled but does not send emails.

So I add a MAILTO line as described in https://wiki.archlinux.org/index.php/...

MAILTO=redact@yahoo.com

*/10 * * * * /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local
44 00,12 * * * /home/wolfv/backup_script /home/wolfv gs://wolfv

cron runs the script as scheduled but does not send emails, and does not leave an entry in the .msmtp.log.

My msmtp is working. This sends email and leaves an entry in the .msmtp.log:

$ cat test.mail | msmtp -a default redact@yahoo.com

How to make cron to send emails?

UPDATE: I got cron to send emails another way. Now I am trying to get cronic to work. Here are the results:


Explicit cronic path and msmtp for specific cron jobs

13:50 crontab with cronic, no email, backup worked:

*/10 * * * * /usr/local/bin/cronic /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local  2>&1 | msmtp redact@yahoo.com

Entry in journalctl:

Jul 29 13:50:01 localhost.localdomain CROND[15058]: (wolfv) CMD (/usr/local/bin/cronic /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local  2>&1 | msmtp redact@yahoo.com)

Nothing in journalctl -u:

$ journalctl -u crond --since 13:50

Entry in .msmtp.log:

Jul 29 13:50:37 host=smtp.mail.yahoo.com tls=on auth=on user=redact@yahoo.com from=redact@yahoo.com recipients=redact@yahoo.com errormsg='the server sent an empty reply' exitcode=EX_PROTOCOL

msmtp for specific cron jobs

14:00 crontab, email sent, backup worked:

*/10 * * * * /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local  2>&1 | msmtp redact@yahoo.com

Entry in journalctl:

Jul 29 14:00:01 localhost.localdomain CROND[16166]: (wolfv) CMD (/home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local  2>&1 | msmtp redact@yahoo.com)

Entry in journalctl -u:

$ journalctl -u crond --since 14:00
-- Logs begin at Sat 2011-12-31 17:00:18 MST, end at Wed 2015-07-29 14:40:40 MDT. --
Jul 29 14:01:01 localhost.localdomain CROND[17142]: (root) CMD (run-parts /etc/cron.hourly)

Entry in .msmtp.log:

Jul 29 14:00:44 host=smtp.mail.yahoo.com tls=on auth=on user=redact@yahoo.com from=redact@yahoo.com recipients=redact@yahoo.com mailsize=2032 smtpstatus=250 smtpmsg='250 OK , completed' exitcode=EX_OK

With MAILTO

14:40 crontab, no email, backup worked:

MAILTO=redact@yahoo.com

*/10 * * * * /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local

Interestingly, this is the only journalctl entry that detected the error I planted in backup_script: line 17:

$ journalctl --since 14:40
Jul 29 14:40:01 localhost.localdomain CROND[20390]: (wolfv) CMD (/home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local)
Jul 29 14:40:01 localhost.localdomain CROND[20387]: (wolfv) CMDOUT (/home/wolfv/backup_script: line 17: error: command not found ...
(more)
edit retag flag offensive close merge delete

Comments

1

Does journalctl -u crond throw any light on this?

javierwilson gravatar imagejavierwilson ( 2015-07-29 06:52:44 -0600 )edit
1

The old and easy way to do that was to set your email destination address on the aliases files for the root account so it will email you every time after a cron job was excecuted, but I have not try it with Fedora 22 you can try it.

aeperezt gravatar imageaeperezt ( 2015-07-29 11:14:50 -0600 )edit

If journalctl -u crond shows no clues, I would say, test /home/wolfv/backup_script /home/wolfv /home/wolfv/backup_local because maybe it's not producing any output. Also check CRONDARGS in /etc/sysconfig/crond is not stopping cronie from sending mails.

javierwilson gravatar imagejavierwilson ( 2015-07-29 13:49:31 -0600 )edit

@javierwilson, journalctl -u crond didn't find anything interesting (I added journalctl output to original post).

This is my crond file:

$ cat /etc/sysconfig/crond
# Settings for the CRON daemon.
# CRONDARGS= :  any extra command-line startup arguments for crond
CRONDARGS=
wolfv gravatar imagewolfv ( 2015-07-29 16:54:02 -0600 )edit
1

@wolfv, who owns the crontab?

NuuN gravatar imageNuuN ( 2015-07-29 17:11:29 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-08-01 06:53:17 -0600

wolfv gravatar image

updated 2015-08-01 07:43:41 -0600

I reinstalled msmtp and then it worked. Once the email was working, cronic was sending email on every cron run, even when there was no error. Which was fixed as follows.

cronic and msmtp are working as intended. The gsutil tool was sending confirmation messages to stderr:

$ /home/wolfv/gsutil/gsutil cp -r /home/wolfv/testSource /home/wolfv/testLocalDest > msg.stdout 2> msg.stderr
$ cat msg.stdout
$ cat msg.stderr
Copying file:///home/wolfv/testSource/README.txt...

And gsutil has an option to quite the confirmation messages:

$ gsutil help options
...
-q    Causes gsutil to perform operations quietly, i.e., without
      reporting progress indicators of files being copied or removed,
      etc. Errors are still reported. This option can be useful for
      running gsutil from a cron job that logs its output to a file, for
      which the only information desired in the log is failures.

$ /home/wolfv/gsutil/gsutil -q cp -r /home/wolfv/testSource /home/wolfv/testLocalDest > msg.stdout 2> msg.stderr
$ cat msg.stdout
$ cat msg.stderr

So I put -q in the backup_script and everything works fine now.

And this is the email when there is an error on line 13 of the backup_script: RESULT CODE: 0

ERROR OUTPUT:
/home/wolfv/backup_script: line 13: uncomment: command not found

STANDARD OUTPUT:
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-07-29 05:20:53 -0600

Seen: 1,525 times

Last updated: Aug 01 '15