shell脚本每隔60秒检查一下名字包含month_rate的程序的数量。如果为0则执行Python脚本发送邮件并退出循环。
#!/bin/bash
while true
do
sleep 60
svc_size=`ps -elf|grep -v grep|grep month_rate|wc -l`
if [ $svc_size == 0 ];then
python /Users/liuguoqiang/Desktop/python/mail.py
break
fi
done
python脚本
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.utils import formataddr
def mail(my_sender, my_pass, recipients):
try:
# 邮件的内容
msg = MIMEText('程序停止了', 'plain', 'utf-8')
# [发件人的邮箱昵称、发件人邮箱账号], 昵称随便
msg['From'] = formataddr([" ", my_sender])
# [收件人邮箱昵称、收件人邮箱账号], 昵称随便
msg['To'] = formataddr([" ", recipients])
# 邮件的主题,也就是邮件的标题
msg['Subject'] = "程序停止了"
# 备注:这里使用的是QQ邮箱的服务器, 加入用腾讯企业邮箱作为发件人的话,请将"smtp.qq.com" 修改为 "smtp.exmail.qq.com"
# 发件人邮箱中的SMTP服务器,qq端口是465
server = smtplib.SMTP_SSL("smtp.exmail.qq.com", port=465)
# (发件人邮箱账号、邮箱密码)
server.login(my_sender, my_pass)
# (发件人邮箱账号、收件人邮箱账号、发送邮件)
server.sendmail(my_sender, recipients, msg.as_string())
server.quit() # 关闭连接
print("邮件发送成功")
except Exception as e:
print("邮件发送失败: ",e)
if __name__ == '__main__':
my_sender = 'liuguoqian@xiaoheiban.cn' # 发件人邮箱账号
my_pass = 'password' # 发件人邮箱授权码 / 腾讯企业邮箱请使用登陆密码
recipients = '2276282410@qq.com' # 收件人邮箱账号
mail(my_sender, my_pass, recipients)
网友评论