Calling mailx from crond

cronmailx

I had a problem that gmail was blocking emails sent using mailx. I solved this by setting up an appropriate ~/.mailrc, which looks like:

set smtp-use-starttls
set nss-config-dir=/home/theuser/.certs
set ssl-verify=ignore
set smtp=smtp://smtp.gmail.com:587
set smtp-auth=login
set smtp-auth-user=xxx
set smtp-auth-password=yyy
set from="rabbitovh@thehologram.com(Rabbit Server)"

So now when I run:

echo "hi" | mailx xxx@gmail.com

my emails send successfully as user and as root.

Now I want cron to work as well. I changed "/etc/sysconfig/crond" to force it to use mailx, with:

CRONDARGS="-m /usr/bin/mailx"

I have ~/.mailrc setup at:

  • /root/.mailrc
  • /home/theuser/.mailrc
  • /etc/.mailrc

But no matter what I do, echo output is not emailed successfully.

The crontab looks like (and I've checked, the scripts are running and doing their job, and echoing, just cron isn't sending emails):

MAILTO="xxx@gmail.com"

# Every minute check processes are running, restart if necessary and send an email.
* * * * * source /home/theuser/.bashrc; global audit_regular

# Every day, send an email describing the state of the host and its jobs.
0 5 * * * source /home/theuser/.bashrc; global audit_daily

# Every Monday at 7am, archive the logs.
0 7 * * 1 source /home/theuser/.bashrc; global archive_logs

Also, this crontab is setup on another host and sending emails fine.

Best Answer

mailx only sends mail if you pass it the destination address on the command line. When you run it with no arguments, it reads interactive commands from its standard input. Beware that your tests fed it garbage which has been interpreted as commands; some of these commands may have corrupted your mailboxes, sent out emails, etc.

Tell mailx to run mailx -t, which expects a full email with headers on standard input.

From a cursory examination, it doesn't look like you can pass a command with parameters via the crond startup script. So write a shell wrapper /usr/local/sbin/mailx-t

#!/bin/sh
exec mailx -t

and put CRONDARGS="-m /usr/local/sbin/mailx-t" in /etc/sysconfig/crond.

Related Question