我这里有个需求,就是服务器会因为不知道的原因(排除断电)导致关机,这种情况下,如果不能及时知道何时关机将会导致不能及时进行重启,并进行安全检查。这里通过撰写 Python 代码并设置服务,监测服务器是否关机并自动邮件通知。本篇以 Ubuntu 为例。
编写 python 代码
编写 /home/jinzhongxu/shutdown_msg.py 模块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
def mail( sender="xxx@qq.com", password="xxxZHyyy", recipients=("jinzhongxu@csu.ac.cn", "otheruser@163.com"), smtp_server="smtp.qq.com", port=465, subject="服务器 IP 地址改变", text="", attachment=("",), ): msg = MIMEMultipart() msg["From"] = _format_addr("JinzhongXu-Pythoner <%s>" % sender) msg["To"] = _format_addr("管理员 <%s>" % ", ".join(list(recipients))) msg["Subject"] = Header(subject, "utf-8").encode() msg.attach(MIMEText(text, "plain", "utf-8"))
attachment = list(attachment) if attachment != [""]: for i, file_path in enumerate(attachment): with open(file_path, "rb") as f: file_dir, file_name = os.path.split(os.path.abspath(file_path)) filename_extension = file_name.split(".") mime = MIMEBase("file", filename_extension[-1], filename=file_name) mime.add_header("Content-Disposition", "attachment", filename=file_name) mime.add_header("Content-ID", f"<{i}>") mime.add_header("X-Attachment-Id", f"{i}") mime.set_payload(f.read()) encoders.encode_base64(mime) msg.attach(mime)
server = smtplib.SMTP_SSL(smtp_server, port) try: server.login( sender, base64.b64decode(password.encode(), altchars=None, validate=False).decode(), ) server.sendmail(sender, list(recipients), msg.as_string()) logs = f"{sender} 给 {'; '.join(recipients)} 的邮件发送成功" except smtplib.SMTPException: logs = "Error: 无法发送邮件" finally: server.quit() return logs
if __name__ == '__main__': subject = "服务器关机" content = "服务器关机了" mail(subject=subject, text=content)
|
设置守护程序
创建服务
1
| sudo vim /etc/systemd/system/mailshutdown.service
|
添加如下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| [Unit] Description=Run command at shutdown
Requires=network.target DefaultDependencies=no Conflicts=reboot.target Before=shutdown.target
[Service] Type=oneshot RemainAfterExit=true ExecStart=/bin/true ExecStop=/usr/local/miniconda/bin/python /home/jinzhongxu/shutdown_msg.py
[Install] WantedBy=multi-user.target
|
设置开机启动
1 2
| sudo systemctl start mailshutdown.service sudo systemctl enable mailshutdown.service
|
这样,当某人使用
等命令关机时,将会收到邮件通知。
参考链接
- ubuntu设置关机时自动执行任务
- 服务器开机或IP改变自动发送通知邮件