Solved

use shell to send email automatically

Posted on 2014-09-23
11
425 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Fine Tune your automatic Updates for Ubuntu / Debian
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…
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…

920 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