Cron send email with STDERR but NOT STDOUT?

g33kz0r picture g33kz0r · Sep 8, 2009 · Viewed 26.6k times · Source

I have some python scripts that run on a daily basis in cron. How can I have cron send me an email ONLY WHEN THERE IS STDERR OUTPUT from my script? I want to be able to mail multiple recipients, and set the subject line individually for each cron entry.

I tried this:

./prog > /dev/null | mail . . . 

but it didn't work -- I still receive blank emails when there is no STDERR. Do I need to do this in the script itself?

Sorry if this seems basic, I have googled a lot but can't seem to find this answered succintly.

Answer

Shizzmo picture Shizzmo · Sep 8, 2009

For cron you don't need to pipe through mail. The cron daemon will automatically mail any output of your command to you. Your crontab entry should look like:

# every minute
* * * * * ./prog >/dev/null

If there is no STDERR output, you won't get any mail.