How to get e-mail from (failed) cron-jobs in Ubuntu?

605

Solution 1

By default, cron will email the owner of the account under which the crontab is running.

The system-wide crontab is in /etc/crontab runs under the user `root'

Because root is used widely, I'd recommend adding a root alias to your /etc/aliases file anyways. (run 'newaliases' after)

The normal way to structure this is for root to be aliased to another user on the system, e.g. for me I'd alias 'root' to 'phil' (my user account) and alias 'phil' to my external email address.

If you have a specific user cron that you'd like emailed to you on output, you can use /etc/aliases again (providing you have superuser access) to redirect the user to another email address, or you can use the following at the top of your crontab:

MAILTO="[email protected]"

If mail should be sent to a local user, you may put just the username instead:

MAILTO=someuser

If you need more information see crontab(5) by running:

man 5 crontab

Solution 2

In order to get email sent from vixie cron you will need something that replicates the sendmail command. So installing postfix or SSMTP will sort this part out. If your using postfix then the aliases file can be used to map system users to real email addresses.

Adding MAILTO="[email protected]" to the top of a crontab will cause any output from the cron job to be emailed. This is regardless of error code.

For scripts that output errors correctly into STDERR then its easy to get emailed only when they go wrong just do this:

MAILTO="[email protected]"
0 5 * * * /bin/some_script > /dev/null

This will redirect just the STDOUT to null. If any STDERR messages are present they will get email to you.

However, I've found some scripts will output errors incorrectly as STDOUT and set the exit code to 1. I have not figure out a way to grab the output from these, but ignore the output if the exit code is 0. The only method I can think of is to redirect the output to a file, then if the exit code is not 0 output that file for cron to grab. Seems pretty horrible though.

Solution 3

If you want to send all output (stdout and stderr) to a specific address then you can use the MAILTO variable. For example, place the following at the top of the script.

MAILTO="[email protected]"

Solution 4

try adding "root: [email protected]" to /etc/aliases

that will send all messages for that user to your email. if you don't want all messages, you could create a user specifically for this.

As long as the script outputs something, you will get a mail.

Solution 5

On all of my productions servers which typically run about 20 cronjobs daily I swear by the python-cronwrap package. Check it out here: http://pypi.python.org/pypi/cronwrap. It's really easy to configure and most of all reliable.

Share:
605

Related videos on Youtube

user674350
Author by

user674350

Updated on September 17, 2022

Comments

  • user674350
    user674350 over 1 year

    i want to add a picture of a field like a football field and on it a ball would roll (just like the football). i have loaded the field's picture in the picturebox , but m nt able to load the second picture i.e of the ball. how can i do this?

  • Miro J.
    Miro J. about 13 years
    And play with the Anchor property.
  • dfrankow
    dfrankow almost 12 years
    Could try habilis.net/cronic
  • bobpaul
    bobpaul over 8 years
    ssmtp implements /bin/sendmail
  • Howard Lee
    Howard Lee almost 7 years
    "for me I'd alias 'root' to 'phil' (my user account) and alias 'phil' to my external email address." How to alias 'phil' to an external email address?