Solaris: how to forward cron e-mails

cronemailsolaris

If a cron job produces output it is e-mailed to the user's account.

I would like to redirect this e-mail to another e-mail account. Preferably on a user-by-user basis.

I've looked into some options that are often mentioned in other postings:

  1. Using the cron MAILTO variable. Won't work. That one is not
    supported on Solaris. It is a Linux thing, potentially also a BSD
    thing, but certainly doesn't exist on Solaris.

  2. Using the ~/.forward file. Can't make that work. I suspect it is
    because this file is really related to sendmail universe and I'm
    not sure Solaris cron actually uses sendmail to send its e-mails.

To get to the bottom of this I guess I need to understand exactly by which mechanism Solaris cron sends e-mails.

Anyone ?

Best Answer

If you want to forward all of a user's mail, not just the mail from Cron, Solaris does support ~/.forward. Solaris also supports global aliases in /etc/mail/aliases; if you modify this file, you need to run newaliases.

If you only want to forward mail from cron, you can set a filter in ~/.forward or /etc/mail/aliases. I don't think Solaris comes with any useful filtering tool preinstalled; the classic program for this is procmail. Use |/usr/local/bin/procmail as your filter, and something like this as your ~/.procmailrc (untested):

:0
* ^From: Cron Daemon <unixhacker2010@yourhostname.example.com>
* ^Subject: Cron .*
! otheraddress@otherhost.example.com

Alternatively, you can mail the output of the job explicitly from the crontab. Install moreutils (I don't know how easy it is to compile under Solaris), which contains a command ifne that executes a program only if its standard input is not empty.

… 2>&1 | ifne mailx -s 'Cron output' otheraddress@otherhost.example.com
Related Question