Ubuntu – How to Configure Cron Mail on Ubuntu

emailUbuntu

On my Debian system I have a traditional local-only "mail" setup. The package install set up some sensible default. (Maybe it prompted me to accept the settings; I don't remember).

EDIT: In Debian 9 and above, exim4-daemon-light is no longer installed as part of the Debian "standard task" (package set).

The main reason this is useful is for errors in cron jobs (and also at jobs), which are delivered as local "mail".

$ aptitude why exim4-daemon-light
i   exim4 Depends exim4-daemon-light | exim4-daemon-heavy | exim4-daemon-custom
$ aptitude why exim4
i   logrotate Depends    cron | anacron | cron-daemon          
i A cron      Recommends exim4 | postfix | mail-transport-agent

Fedora Workstation does not include a functioning mail setup. You have to choose to install it, and you probably need to do some extra configuration.

Ubuntu is based on Debian. On Ubuntu Desktop, assuming you installed cron (or at), will you have a (working) local mail setup for it?

EDIT: I guess on Debian 9 you already need to choose to install a mail system if you want one, despite the "Recommends".

(I have written some Ansible stuff which sets up cron jobs on a Debian system. I expect to switch the system to Ubuntu Desktop eventually. So I am thinking about the assumptions my scripts are making).

Best Answer

Ubuntu Desktop 18.04 does not include cron.

If you do install cron, it will not automatically install a mail transport agent. The Ubuntu cron package no longer "Recommends" any "mail transport agent" packages.

(This feels like another inconsistency, as the package description for cron still says "you should probably install a mail system as well so that you can receive these messages". They didn't bother adding a Suggests in place of the Recommends... probably because no-one would really notice if they did anyway.)

Also, there is a link in the question about pam_mail. It looks like a default install of Ubuntu Desktop 18.04 still includes pam_mail lines in /etc/pam.d/* - unlike Fedora Workstation.

If you do not need mail specifically, you might be interested in systemd-cron. Apparently it logs cronjob output to the system log. If a mail system is available, it additionally sends any output as a mail.

Patches were submitted in 2012 to provide similar feature in the Debian cron package. The description of the patch says that by default, it will only log output if it could not send a mail. So far, the ticket has not been closed. So at the moment, if a cronjob generates output, the specific output may be lost. This is logged as an informational message; it is not marked as a warning or an error.

Jul 27 16:36:01 debian9-vm CRON[2337]: pam_unix(cron:session): session opened for user root by (uid=0)
Jul 27 16:36:01 debian9-vm CRON[2338]: (root) CMD (echo fail; exit 1)
Jul 27 16:36:01 debian9-vm CRON[2337]: (CRON) info (No MTA installed, discarding output)
Jul 27 16:36:01 debian9-vm CRON[2337]: pam_unix(cron:session): session closed for user root
Related Question