Operating System - HP-UX
1752294 Members
4750 Online
108786 Solutions
New Discussion юеВ

Re: Not sending mail when body is empty

 
SOLVED
Go to solution
John Peace
Frequent Advisor

Not sending mail when body is empty

I remotely administer aprox 20 servers located all over the world. I have database log files I grep for a specific error message. I then pipe the output to mailx and mail it to the root aliases. I am learning perl, but in the meantime I need to get info from these files. My problem is: I don't want a mail message sent if there are no errors. Here is what I am currently doing.

grep "SYSTEM ERROR" /backup/temp/travel.`date +'%b%d'`.lg | mailx -s "Log Errors at Lincoln" root

Is there a way for mailx to not send the mail if the grep returns nothing? I know it will send mail with -s, but I need to know at a quick glance which server it came from. Any suggestions will be appreciated

9 REPLIES 9
Jeff Schussele
Honored Contributor

Re: Not sending mail when body is empty

Hi John,

Why don't you let the grep create (or not) the file. Then test (-f or -e) for the existence of it and whether it's 0 bytes (-s) or not before sending it to mailx?

Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
James R. Ferguson
Acclaimed Contributor

Re: Not sending mail when body is empty

Hi John:

Try this:

# grep -q "SYSTEM ERROR" /backup/temp/travel.`date +'%b%d'`.lg || mailx -s "Log Errors at Lincoln" root

Notice the quiet option ('-q') and the double pipe.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Not sending mail when body is empty

Hi (again) John:

Oh, sorry, you *don't want to send the mail if 'grep' fails to match, so:

# grep -q "SYSTEM ERROR" /backup/temp/travel.`date +'%b%d'`.lg && mailx -s "Log Errors at Lincoln" root

Regards!

...JRF...
Jeff Schussele
Honored Contributor

Re: Not sending mail when body is empty

JRF....I was wondering...... ;~)

I tried that and had fun watching my prompt not come back, then had to ctrl-C it twice.

Note: Sometimes I really enjoy HP-UX's attempt at levity.
When I interrupted it first time it came back:
(Interrupt -- one more to kill letter)

Then when I tested just what it would do in the original scenario it responded:
Null message body; hope that's ok

Have a good weekend,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
John Peace
Frequent Advisor

Re: Not sending mail when body is empty

Jeff,

I tried it also and had to interrupt it also. I already have it the way I wrote, but a mail message is sent with

Null message body; hope that's ok

As the text. So, I get a mail message whether I have an error or not. I want a mail message only when grep returns something. Can you post and example of what you were suggesting?

TIA
James R. Ferguson
Acclaimed Contributor

Re: Not sending mail when body is empty

Hi (again) John:

OK, its definitely Friday. Try this. It avoides the "dead letter" or "null body":

# grep -q "SYSTEM ERROR" /backup/temp/travel.`date +'%b%d'`.lg && mailx -s "Log Errors at Lincoln" root < /dev/null

Regards!

...JRF...
John Peace
Frequent Advisor

Re: Not sending mail when body is empty

I see what is happening now. It sends me a blank email if grep finds something. Now how can I get it to send the output with it, but still nto send an email if there is not output?It would be nice if I could see the errors found. I could then decide which fires to put out first.

Thanks
James R. Ferguson
Acclaimed Contributor
Solution

Re: Not sending mail when body is empty

Hi John:

OK, do this to receive the output if it contains something of interest:

# MYFILE=/backup/temp/travel.`date +'%b%d'`.lg

# grep -q "SYSTEM ERROR" $MYFILE && mailx -s "Log Errors at Lincoln" root < $MYFILE

Regards!

...JRF...

John Peace
Frequent Advisor

Re: Not sending mail when body is empty

James,

Thanks. With your help, here is what I am going to do.

INFILE=/backup/temp/travel.`date +'%b%d'`.lg
OUTFILE=/tmp/errors.out

grep "SYSTEM ERROR" $MYFILE > $OUTFILE && mailx -s "Log Errors at Lincoln" root < $OUTFILE

This will put the errors in the body of the email. Thanks again

John