Cron jobs are a vital part of every sysadmins setup. Backups, log rotation, certificate renewal and whatnot.
I've mostly just relied on them working as they should until now, but with my recent Huginn + Matrix obsession I figured I should have some sort of notifications.

Crontab supports sending emails for your jobs, all you have to do is add MAILTO=youremail@example.com at the top. It defaults (on most distros) to sendmail, which I discovered was kind of a pain to set up. Instead I found these instructions for sSMTP.

I already had an account at Mailgun for an old app so I just had to add my domain. Mailgun recommends using a subdomain so in my case I went with mg.jonnev.se. I configured sSMTP with my Mailgun credentials following the guide above and it worked just fine!

Now getting emails for everything is kinda annoying so using Mailguns routes I forwarded all incoming emails for the address I setup in crontab to a webhook in Huginn. In the Mailgun examples you can see the JSON for a forwarded email.

I then added an EventFormattingAgent in Huginn with these attributes

{
  "instructions": {
    "message_html": "<b>{{bin.cmd}}</b><br>{{body-plain}}",
    "message": "{{bin.cmd}} {% line_break %} {{body-plain}}"
  },
  "matchers": [
    {
      "path": "{{subject}}",
      "regexp": ">\\s(?<cmd>.*)",
      "to": "bin"
    }
  ],
  "mode": "clean"
}

Matrix supports HTML formatting with a fallback for plain text. There should be a <pre> around the message_html above but Ghost wouldn't let me...

The result looks like this:

Cron output in Matrix

You could easily use this to send notifications to Slack, Pushover or any of the other services supported by Huginn. Or just an outbound webhook with PostAgent, which is what I use to post to my Matrix room.

Hint: To learn how to control the output of commands in cron I found this post helpful

Hint 2: To silence certbot (in the screenshot above) I'm now using this!