import os
import smtplib
from email.MIMEText import MIMEText
from email.Header import Header
mailto_list=["aa@qq.com"]
mail_host="smtp.qq.com"
mail_user="aa@qq.com"
mail_pass="****"
mail_postfix="qq.com"
cmd_="grep -wn 'ERROR' /home//log.log"
def cmd(cmd_):
result = os.system(cmd_)
if result == 0:
return True
return False;
def send_mail(to_list,sub,content):
me="hello"+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content,_subtype='plain',_charset='gb2312')
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
server = smtplib.SMTP()
server.connect(mail_host)
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == "__main__":
if cmd(cmd_) == True:
print "error"
send_mail(mailto_list,"log","error exists in /home/log.log")
else:
print "ok"
send_mail(mailto_list,"log","no error exists in /home/log.log")
可以在linux 定时器中设置 crontab -e
30 3 * * * python /check.py
每天3点半执行操作