0

I wrote a shell script that I use under ash, and I redirect stderr and stdout to a log file. I would like that log file to be emailed to me only if stderr is not empty.

I tried:

exec >mylog.log 2>&1

# Perform various find commands

if [TEST_IF_STDERR_NOT_EMPTY]; then
/usr/bin/mail -s "mylog" email@mydomain.com < mylog.log
fi

My question is twofold:

1- I get a -sh: /usr/bin/mail: not found error. It seems that the mail command doesn't exist under ash (or at least under my linux box, which is a Synology NAS), what would be the alternative? Worst case, perl is available, but I would prefer to use standard sh commands.

2- How to I test that stderr is not empty?

Thanks

1 Answers1

0

How to check if file is empty in bash

As for the first question, in your code you are calling mail but lower in the post you are calling email. Check your code and make sure it is mail.

Use which mail to get the full path. Maybe it is not installed in /usr/bin/. Use find to locate mail. If you can go to another shell, run it and then execute which mail to get the full path of mail in case the path is set up in the alternative shells.

Community
  • 1
  • 1
Hameed
  • 2,227
  • 1
  • 21
  • 31
  • There was a typo in my question. It did say "mail" and NOT "email". I've corrected it. Furthermore, I had already tried which (didn't return anything) and locate (returned `-sh: locate: not found`) to no avail. Finally, the thread you pointed me to doesn't work for me because it checks if a file is empty, but in my case, the log isn't empty, it contains both stderr AND stdout and I only want to know if stderr is not empty (the log file is never empty because it always have stdout). – TechnologyGeek Jan 10 '14 at 14:42
  • Try mailx. If you cannot find it, install it. – Hameed Jan 13 '14 at 15:50
  • I didn't find mailx, but I manager to get the email part going using sendmail. However, regarding my SECOND question, I tried `if [ -s 2 ]; then` to test is stderr was empty, but that doesn't seem to work. I think it is because 2 is redirected to my log file, so it is empty. How can I test if there is something in stderr? – TechnologyGeek Jan 14 '14 at 01:54
  • Write stderr to a separate log file and when you are done checking it again append it to the end of your main log file. Any other way it will be too messy. Though to use Perl? – Hameed Jan 19 '14 at 23:55