?
Solved

use shell to send email automatically

Posted on 2014-09-23
11
Medium Priority
?
438 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
11 Comments
 
LVL 6

Expert Comment

by:xeroxzerox
ID: 40340918
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
ID: 40341087
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 1000 total points
ID: 40341146
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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 

Author Comment

by:beardog1113
ID: 40341170
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 1000 total points
ID: 40341179
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
 
LVL 5

Assisted Solution

by:Dave Gould
Dave Gould earned 1000 total points
ID: 40341187
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
ID: 40341207
# 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 1000 total points
ID: 40341242
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
ID: 40341257
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
ID: 40341502
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
ID: 40341554
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses
Course of the Month14 days, 21 hours left to enroll

770 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