Shell Script Basics (vi)

Source: Internet
Author: User
Tags email account

First, alarm system mail engine
[[email protected] mon]# lsbin conf log mail shares[[email protected] mon]# cd mail[[email protected] m ail]# vim mail.py//Create send mail script #!/usr/bin/env python#-*-coding:utf-8-*-import os,sysreload (SYS) Sys.setdefaultenco Ding (' UTF8 ') import getoptimport smtplibfrom email. Mimetext Import mimetextfrom Email. Mimemultipart Import mimemultipartfrom subprocess import *def sendqqmail (Username,password,mailfrom,mailto,subject,        Content): Gserver = ' smtp.qq.com ' gport = try:msg = Mimetext (Unicode (content). Encode (' Utf-8 '))        Msg["from" = Mailfrom msg[' to '] = mailto msg[' reply-to '] = mailfrom msg[' Subject '] = Subject SMTP = Smtplib. SMTP (Gserver, Gport) smtp.set_debuglevel (0) Smtp.ehlo () Smtp.login (Username,password) Smtp.sen DMail (Mailfrom, mailto, msg.as_string ()) Smtp.close () except Exception,err:print "Send Mail failed. Error:%s "% errdef Main (): To=sys.argv[1] Subject=SYS.ARGV[2] content=sys.argv[3]# #定义QQ邮箱的账号和密码, you need to change your own account and password (please do not put the real user name and password on the Internet, otherwise you will die very miserable) sendqqmail (' [email& Nbsp;protected] ', ' aaaaaaaaaa ', ' [email protected] ', to,subject,content) if __name__ = = "__main__": Main () ##### Script usage Instructions ###### #1. First, define the email account number and password # # in the script. Script execution commands are: Python mail.py target Mailbox "message subject" "Message content" [[[email protected] mail]# vim mail.sh//Create script mail.sh when system service is abnormal Will call mail.sh#! /bin/bashlog=$1t_s= ' Date +%s ' t_s2= ' date-d "2 hours ago" +%s ' if [!-f/tmp/$log]then echo $t _s2 >/tmp/$logfit _s2= ' Tail-1/tmp/$log |awk ' {print '} ' echo $t _s>>/tmp/$logv =$[$t _s-$t _s2]echo $vif [$v-gt 3600]then./mail.py $     1 $0 echo >/tmp/$log. txtelse if [!-f/tmp/$log. txt] then echo "0" >/tmp/$log. txt fi  Nu= ' cat/tmp/$log. txt ' nu2=$[$nu +1] echo $nu 2>/tmp/$log. txt if [$nu 2-gt] then./mail.py   $ "Trouble Continue min $" "$ $" echo "0" >/tmp/$log. txt Fifi
Second, Operation Alarm system
[[email protected] ~]# crontab -e      //创建计划任务* * * * * cd /usr/local/sbin/mon/bin; bash main.sh

Shell Script Basics (vi)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.