Reputation: 16321
I've seen many questions about only emailing STDERR, but that's not quite what I want. When STDERR is not blank, I would like both STDOUT and STDERR to be sent to me, or, alternatively, the contents of a log file, which would accomplish the same thing. How can I accomplish this?
Upvotes: 1
Views: 82
Reputation: 113834
In summary, you want to send a logfile by email if the STDERR output is not blank. In that case:
some_command 2>"$HOME/.errs.tmp.$$"
[ -s "$HOME/.errs.tmp.$$" ] && mail user@host -s "Error Info" <logfile
-s
tests whether the file is nonempty. So, the email is sent only if there were errors.
To clean up the temporary files, use this:
errs="$HOME/.errs.tmp.$$"
my_exit() { rm -f "$errs"; }
trap my_exit EXIT
some_command 2>"$errs"
[ -s "$errs" ] && mail user@host -s "Error Info" <logfile
Note that the temporary file was put in the user's home directory. Although probably not important here, this avoids the security issues associated with created temporary files in /tmp
and hence the need for mktemp
or other utility.
Upvotes: 1