Cron email output only when error occurs

in CentOS 6.3, I have “/sbin/service iptables save” in cron. How do I set cron to only email when there is an error or, if possible, run “service” silently (unless there is an error)? If none of these options are possible, is running “iptables-save > /etc/sysconfig/iptables” instead the only remaining choice?

Thanks

Howdy,

Cron will email you with any output produced by the programs it’s running.

So the key would be to make sure the program you’re running isn’t outputting anything unless it’s an error.

You can accomplish that by redirecting the standard output… something like this:

/sbin/service iptables save >/dev/null

With that, all normal output will be redirected to /dev/null, but warning/error output will still be generated (and thus emailed).

-Eric

for future reference, I think you meant >/dev/null 2>&1

thanks!

Howdy,

Well, the forum mangled what I typed, but I meant just:

/dev/null

What you mentioned there, " >/dev/null 2>&1", will cause all output, both regular and errors, to go to /dev/null… and it didn’t sound like that’s what you wanted.

By using just “>/dev/null”, only standard output is redirected to /dev/null, and errors will still be output, which will cause them to be emailed to you.

-Eric

correct, thanks!

Please note though that this method of course only works if the script/application actually does send regular output to stdout and warnings/errors to stderr. :slight_smile: Of course, a sane script should do that.

This doesn’t work for me on CentOS 6.3 and 6.4; not sure why. Please let me know if you did anything additional to get it to work.