Solved

use shell to send email automatically

Posted on 2014-09-23
11
430 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 250 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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 250 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 250 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 250 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Ubuntu Apache Webserver - File Permissions 5 77
Moving from Mcrypt to OpenSSL 18 66
trouble on installing syslog-ng on CentOS 7 7 101
Migrating a Linux server to VMware 3 113
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 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