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
Solved

use shell to send email automatically

Posted on 2014-09-23
11
429 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
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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding a continue option in a script 9 53
AD Cleanup by EmployeeID 11 61
CENTOS DHCP Server / PXE/TFTP 14 152
Samba 4, Users Permission, 5 44
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…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…
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…

809 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