Anacron email setup on CentOS 8/Rocky Linux 8

Thu, Apr 21, 2022 2-minute read

Introduction

When you have a cron job that runs, then sometimes you want the cron process to send an email, in case something goes wrong or if you have set your cronjob to produce output.

This happens automatically if your system is configured correctly, but if you like me just install a vanilla CentOS or Rocky and expect stuff like this to work, you have a couple of small extra tasks to perform.

Prerequisites

To be able to send mails, the server need two pieces of software - a MTA and a mail client.

I am using mailx which emulates sendmail as a client - and also Postfix - I suggest you use these packages since they are small.

To install these packages you simply run:

sudo dnf install -y mailx
sudo dnf install -y postfix

When the software is installed verify that you can send a mail by doing echo "test" | mailx -s "test subject" youremail@hostname.com

If you receive the mail, then everything is in place for you to receive cron mails - if not, check if the mail server is configured correctly - that is done in the file /etc/mail.rc.

My mail.rc contains the line:set smtp=smtp://mail.root.dom:25 which tells postfix to send via that mail server.

If you still have set this up, please check the syslog and verify that nothing is blocking the outgoing mails.

Step by step guide

When you have verified that you can send mails from the machine, then you need to configure the user that runs the cron jobs to receive email - this is done in /etc/aliases

My file contains: root: bjornsmith@gmail.com as the last line, which tells the system that all mail for root should go to that email address. If you have cronjobs running under a different username, then you just add a line username: email@hostname.com.

  1. Update /etc/aliases as described above
  2. execute sudo newalises
  3. execute sudo systemctl restart postfix

When you have done these 3 last steps and prerequisites are in order and you can actually send mails, then you should automatically get an email when a cronjob is run and that job outputs anything to std out or std error.