Question

I have a script, which we will call script.sh that writes to both stdout and stderr.

I want to pipe the output (stdout) of this script to mailx, but I don't want any email from cron.

Here's my crontab, set to 4:00 PM daily:

00 16 * * * /path/to/script.sh | mailx name@domain.com >/dev/null 2&>1

Via mailx I get the script's stdout output, but I still get an email from cron containing the script's stderr output.

I've also tried stripping away stderr on the left side of the pipe, as follows:

00 16 * * * /path/to/script.sh 2> /dev/null | mailx name@domain.com

This results in cron sending me an email that says:

Null message body; hope that's ok

Actually, dear cron, that's not okay with me.

Any ideas on how to accomplish this?

edit: Another interesting experiment...

00 16 * * * test -d /

This has no output whatsoever, yet I get an email from cron saying that the message subject and body are empty... so why send it? :\

Was it helpful?

Solution 2

You receive the mail any time the cron command has some output (stdout or stderr). The message "Null message body; hope that's ok" is a warning from the mailx command. The following modification will suppress all the warnings from mailx:

00 16 * * * /path/script.sh 2> /dev/null | mailx name@domain.com > /dev/null 2>&1

Notes:

  1. Be aware that if there is any error sending the email, you will not get any report about it.
  2. Are you sure the stderr of script.sh shouldn't be mailed too? If yes, use 2>&1 instead of 2> /dev/null.

OTHER TIPS

It's a bit hacky...but you can set the MAILTO variable to an empty value so that cron is not sending mail at all.

Open the crontab file with:

crontab -e

and at the top simply prepend:

MAILTO=""

I don't know if it goes against some of your constraints.

If you want to do it just for your script you may try something like that in your script:

#!/bin/sh

OLDMAILTO=$MAILTO

MAILTO=""

# do your stuff here

MAILTO=$OLDMAILTO
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top