LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   SUSE / openSUSE (https://www.linuxquestions.org/questions/suse-opensuse-60/)
-   -   send an email of script output (https://www.linuxquestions.org/questions/suse-opensuse-60/send-an-email-of-script-output-742296/)

s_linux 07-23-2009 02:13 PM

send an email of script output
 
Hi All,

I'm trying to send some file which generated by script to my email. when I run the script I'm getting an email. Thats fine. But it seems to be all messed up like below

Memory Status on ServerA: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!! CPU Status on ServerA: Cpu(s): 1.8%us, 2.3%sy, 0.1%ni, 94.1%id, 0.6%wa, 0.0%hi, 1.1%si, 0.0%st DEV POrt are OK on ServerA. Service is running. Memory Status on ServerB: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!! CPU Status on ServerA: Cpu(s): 1.8%us, 2.3%sy, 0.1%ni, 94.1%id, 0.6%wa, 0.0%hi, 1.1%si, 0.0%st DEV POrt are OK on ServerB. Service is running.on ServerB


I want to make it seperate lines like below

Memory Status on ServerA: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!!
CPU Status on ServerA: Cpu(s): 1.8%us, 2.3%sy, 0.1%ni, 94.1%id, 0.6%wa, 0.0%hi, 1.1%si, 0.0%st
DEV POrt are OK on ServerA.
Service is running on ServerA.


Memory Status on ServerB: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!!
CPU Status on ServerB: Cpu(s): 1.8%us, 2.3%sy, 0.1%ni, 94.1%id, 0.6%wa, 0.0%hi, 1.1%si, 0.0%st
DEV POrt are OK on ServerB.
Service is running on ServerB.

Is there anyway to do so in the script. Thanks.

catkin 07-23-2009 02:30 PM

Hello s_linux :)

How exactly are you sending the mail in your script? It looks like you missed off the double quotes.

Best

Charles

s_linux 07-23-2009 02:53 PM

Thanks for your reply. I'm sending mail with following command in my script ..

mail -s "Health Check Status" uname@company.com < /home/uname/Result

Thanks again.

colucix 07-24-2009 01:20 AM

You can try either a here document or process substitution, using cat to print out the file content:
Code:

$ mail -s "Health Check Status" uname@company.com << EOF
> $(cat /home/uname/Result)
> EOF
$
$ mail -s "Health Check Status" uname@company.com < <(cat /home/uname/Result)
$


catkin 07-24-2009 02:46 AM

Quote:

Originally Posted by s_linux (Post 3618061)
Thanks for your reply. I'm sending mail with following command in my script ..

mail -s "Health Check Status" uname@company.com < /home/uname/Result

Thanks again.

That ought to work. What do the contents of /home/uname/Result look like? If they don't look OK then how are you creating the file?

s_linux 07-24-2009 08:18 AM

Thanks for your replies..

$ mail -s "Health Check Status" uname@company.com << EOF
> $(cat /home/uname/Result)
> EOF
$
$ mail -s "Health Check Status" uname@company.com < <(cat /home/uname/Result)
$

I tried this but still the same thing. email message looks very messy.

That ought to work. What do the contents of /home/uname/Result look like? If they don't look OK then how are you creating the file?

my intial post was actuvally /home/uname/Result file. Im generating this file as script output.

in the file it looks good. But in email message only messy.

Can we just attach the file instead. Thanks

rn_ 07-24-2009 08:33 AM

it could be your email client. are you by any chance using a windows-based email client?

s_linux 07-24-2009 09:03 AM

I'm using MS outlook. does it matters? If so How can I get nicer message. Thanks

catkin 07-24-2009 09:10 AM

Hello s_linux :)
Quote:

Originally Posted by s_linux (Post 3618833)
[snip]
in the file it looks good. But in email message only messy.

Can we just attach the file instead. Thanks

AFAIK the mail command does not support attachments.

Have replicated your method and it worked as you want:
Code:

c@CW8:~$ cat Result
Memory Status on ServerA: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!!
CPU Status on ServerA: Cpu(s): 1.8%us, 2.3%sy, 0.1%ni, 94.1%id, 0.6%wa, 0.0%hi, 1.1%si, 0.0%st
DEV POrt are OK on ServerA.
Service is running on ServerA.


Memory Status on ServerB: Mem: 3867444k total, 862680k used, 3004764k free, 54456k buffers!!
CPU Status on ServerB: Cpu(s): 1.8%us, 2.3%sy, 0.1%ni, 94.1%id, 0.6%wa, 0.0%hi, 1.1%si, 0.0%st
DEV POrt are OK on ServerB.
Service is running on ServerB.
c@CW8:~$ mail -s "Health Check Status" root < Result
c@CW8:~$ dpkg -l mailx | grep mailx
ii  mailx                                      1:8.1.2-0.20071017cvs-2                              A simple mail user agent

Mail was received (in MS Outlook) looking just fine.

Time to dig into the details. Mail/mailx/mail has been around for a long time and is (?) no longer being developed. It is unlikely that the different behaviour on your system and mine is caused by features of different versions.

Stupid question first. How are you viewing the mail when you see it with line ends and empty lines replaced with spaces?

Do you have a ~/.mailrc file. If so, rename it to something else and try again, to see if it's something in there that's causing the problem.

What does your /etc/mail.rc file look like. Here's mine, not touched since installation
Code:

c@CW8:~$ cat /etc/mail.rc
set ask askcc append dot save crt
ignore Received Message-Id Resent-Message-Id Status Mail-From Return-Path Via Delivered-To

Best

CHarles

s_linux 07-24-2009 09:46 AM

If I do cat Result, I got similar result as you got but in email message it looks messy.

I dont see any ~/.mailrc file in my server.

Here is the output of mail.rc file

$ cat /etc/mail.rc
set asksub append dot save crt=20
ignore Received Message-Id Resent-Message-Id Status Mail-From Return-Path Via

I just changed to mail.rc file as you have but no luck. Thanks

rn_ 07-24-2009 09:52 AM

The line termination characters are different in unix and windows, and that sometimes causes a problem. I think a lot of newer win apps are able to handle the nix EOL, but there are still some out there (eg. notepad) that won't handle it. if you're using an older version of outlook it might still have the problem.

if attaching the doc works for you, you could always uuencode it and pass it to the mail prog, like so:

Code:

uuencode /home/uname/Result Result.txt | mail -s "Health Check Status" uname@company.com
for some reason my distro doesn't have uuencode, but i found a gmime pkg that does the same; instead of uuencode use gmime-uuencode.

rn_ 07-24-2009 09:56 AM

you could also try:

Code:


cat /home/uname/Result | unix2dos | mail -s "Health Check Status" uname@company.com


catkin 07-24-2009 10:01 AM

Quote:

Originally Posted by rn_ (Post 3618969)
The line termination characters are different in unix and windows, and that sometimes causes a problem. I think a lot of newer win apps are able to handle the nix EOL, but there are still some out there (eg. notepad) that won't handle it. if you're using an older version of outlook it might still have the problem.

if attaching the doc works for you, you could always uuencode it and pass it to the mail prog, like so:

Code:

uuencode /home/uname/Result Result.txt | mail -s "Health Check Status" uname@company.com
for some reason my distro doesn't have uuencode, but i found a gmime pkg that does the same; instead of uuencode use gmime-uuencode.

Interesting suggestions. FWIW, I routinely send reports generated by smartmontools on ubuntu to myself using "mail" and they look OK in Outlook 2003.

I've never used uuencode. Will that be readable by most email readers?

catkin 07-24-2009 10:07 AM

@s_linux :)

Can you send a copy to yourself on the Linux system and see what it looks like there? It would help to know when the mangling happens -- on the Linux system or later. The mail -c option followed by your username (without any @domain_name) should do it.

What are you using to send the mail from your Linux system to wherever? postfix, sendmail ... ?

rn_ 07-24-2009 10:09 AM

Quote:

Originally Posted by catkin (Post 3618979)
Will that be readable by most email readers?

yes AFAIK. uuencode is a pretty standard way to attach docs. it works with my outlook client.


All times are GMT -5. The time now is 11:46 AM.