Reputation: 147
I need to send emails only when I have anything in stdout. Right now I send mails every 10 minutes configured in cron like this, even if the script returns nothing:
/root/script.sh|mail -s topic [email protected]
How can I skip sending mails on null stdout?
Upvotes: 1
Views: 2559
Reputation: 2468
I found even a better solution. Use the -E option for the mail command and it won't send e-mails if the body is empty. The manual for mail states the following.
-E Do not send messages with an empty body. This is useful for
piping errors from cron(8) scripts.
So just like that.
/root/script.sh|mail -E -s topic [email protected]
Upvotes: 7
Reputation: 63892
You can try:
message=$(/root/script.sh) && [[ ! -z "$message" ]] && mail -s topic [email protected] <<< "$message"
Meaning:
script.sh
to an variablemessage
Upvotes: 2