Solved

use shell to send email automatically

Posted on 2014-09-23
11
421 Views
Last Modified: 2014-09-24
Hi All
    i have one text file a.log at /usr/local/nagios/var , i would like to use Shell to write script and send the a.log file as attachment to me email everyday, how to write the script and how to set the regular sending? my server is linux centos6.5
0
Comment
Question by:beardog1113
  • 5
  • 3
  • 3
11 Comments
 
LVL 6

Expert Comment

by:xeroxzerox
Comment Utility
Hi,

you can use heirloom-mailx  & mailx for send attachment email.
First you need to install mailx in your machine.

# yum install -y mailx
Set an External SMTP Server to Relay E-Mails
# vi /etc/mail.rc and edit

set smtp=smtp://smtp.example.com:543
# tell mailx that it needs to authorise
set smtp-auth=login
# set the user for SMTP
# set smtp-auth-user=user@domain.tld
set smtp-auth-user=user.name@example.com
# set the password for authorisation
set smtp-auth-password=enter-password-here-1234

save and exit
Here are some of the available options :

    * -a file Allows you to attach the given file to the e-mail
    * -b address Sends blind carbon copies to the comma separated e-mail address list
    * -c address Sends copies to a list of users
    * -q file Sets the message contents from the given file
    * -r from address Sets the from address of the e-mail to be sent
    * -s subject Sets the e-mail subject

Sending a message with an attachment:
echo "Message" | mail -s "Subject" -a /loc/to/attachment.txt email@address

if you need it in full details then please see below link.
https://www.digitalocean.com/community/tutorials/how-to-send-e-mail-alerts-on-a-centos-vps-for-system-monitoring
0
 

Author Comment

by:beardog1113
Comment Utility
thank you, i just add new job in crontab, "1 * * * * /usr/local/nagios/var/sendemaildaily.sh", also i created sendemaildaily.sh, "sendmail 1324514@qq.com < /usr/local/nagios/var/nagios.log
", but it was not working, how should i write the sendemaildaily.sh scirpt?
0
 
LVL 5

Assisted Solution

by:Dave Gould
Dave Gould earned 250 total points
Comment Utility
change it to:
mailx -s "Daily logs" 12234514@qq.com < /usr/local/nagios/var/nagios.log

You also said that you wanted the log as an attachment so you could try this:

cd /usr/local/nagios/var
uuencode nagios.log nagios.log | mailx  12234514@qq.com
0
 

Author Comment

by:beardog1113
Comment Utility
thank you, now the sendemaildaily.sh is working i can run this script succesfully, and i can get the email. but after i ask crontab to run this, it is not working, the crontab setting is "1 * * * * /usr/local/nagios/var/sendemaildaily.sh", what can i do?
0
 
LVL 6

Assisted Solution

by:xeroxzerox
xeroxzerox earned 250 total points
Comment Utility
mailx -s "Subject" -a /usr/local/nagios/var/nagios.log  12234514@qq.com

and 2nd is

heirloom-mailx -s "Log:Details" 12234514@qq.com < /usr/local/nagios/var/nagios.log
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 5

Assisted Solution

by:Dave Gould
Dave Gould earned 250 total points
Comment Utility
So you have asked for it to run 1 minute after every hour. Have you waited long enough?
Whose crontab is it in? Does that user have mail mailx in his path (should install in /bin )?

If in doubt, add a log file to you crontab (you should do this anyway to avoid unwanted mails).

ie:
1 * * * * /usr/local/nagios/var/sendemaildaily.sh > /tmp/sendmaildaily.log 2>&1
0
 

Author Comment

by:beardog1113
Comment Utility
# crontab -u root -l is "1 * * * *  /usr/local/nagios/var/sendemaildaily.sh /tmp/sendmaildaily.log 2>&1"

#vim /usr/local/nagios/var/sendemaildaily.sh  is "#!/bin/sh   sendmail 1324514@qq.com  /usr/local/nagios/var/nagios.log"

i van run the sendemaildaily.sh manually, but autorun is not working
0
 
LVL 6

Accepted Solution

by:
xeroxzerox earned 250 total points
Comment Utility
do simple step..

#crontab -e
For one minute
*/1 * * * * sh /usr/local/nagios/var/sendemaildaily.sh
Note: give executable permission to script Like: chmod +x /usr/local/nagios/var/sendemaildaily.sh
that's it
0
 
LVL 5

Expert Comment

by:Dave Gould
Comment Utility
You should have a ">" between the sendemaildaily.sh and /tmp/......
Once you've run the script via crontab, the /tmp/..... file will probably give some indication as to why the job is not running.
A presume that the crond deamon is running ?
0
 

Author Comment

by:beardog1113
Comment Utility
the log file has been created now, the error is "/usr/local/nagios/var/sendemaildaily.sh: line 2: sendmail: command not found", for crontab setting, "*/1 * * * * sh /usr/local/nagios/var/sendemaildaily.sh", i have not write "sh" after "* * * *", i think that is why the log file has not been created before. now how to fix sendmail command not found? thank you alll
0
 

Author Comment

by:beardog1113
Comment Utility
Hi All
    after i use mailx -s "Subject" -a /usr/local/nagios/var/nagios.log  12234514@qq.com instead of sendmail command, it is working now
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now