利用python制作在Linux服务器后台定时运行的任务-邮件提醒

简介: 1. 自动任务的功能为:   定时扫描数据库中的记录,然后发邮件 代码如下 scheduleMail.py import pymysql import smtplib from email.

1. 自动任务的功能为:

  定时扫描数据库中的记录,然后发邮件

代码如下

scheduleMail.py

import pymysql
import smtplib  
from email.mime.text import MIMEText  
from email.header import Header 
import time

def sendMail(body):
    sender = 'xxx@163.com'  
    receiver = ['abc@xxx.com', 'def@xxx.com', 'ghi@xxx.com'] 
    subject = '邮件主题'  
    smtpserver = 'smtp.163.com'  
    username = 'your username'  
    password = 'your password'  
    
    msg = MIMEText(body,'plain','utf-8') #中文需参数‘utf-8',单字节字符不需要  
    msg['Subject'] = Header(subject, 'utf-8')  
    msg['From'] = 'xxx<xxx@163.com>'    
    msg['To'] = "abc@xxx.com', 'def@xxx.com', 'ghi@xxx.com"  
    smtp = smtplib.SMTP()  
    smtp.connect('smtp.163.com')  
    smtp.login(username, password)  
    smtp.sendmail(sender, receiver, msg.as_string())  
    smtp.quit()  

def scanLogic():
    conn = pymysql.connect(host='服务器IP', user='数据库用户名', passwd='数据库密码', db='数据库名', port=3306, charset='utf8')
    cur = conn.cursor()
    
    sql = "select * from ..."
    
    cur = conn.cursor()
    cur.execute(sql)
    alldata = cur.fetchall()
    mailBody = ""
    separator = "----------------------------------------------\n"
    for rec in alldata:
        field1 = rec[0]
        field2 = rec[1]
        line = "field1: %s \t field2: %s \n" % (field1, field2)
        mailBody = mailBody + line + separator
    
    print('邮件正文: %s' % mailBody)
    if (mailBody != ""):
        sendMail(mailBody)
    else:
        print("无可发送邮件")

def main():
    while (True):
        time.sleep(1800)
        scanLogic()

main()

 

2. 把它做成后台任务的shell脚本如下

scheduleMail.sh

#!/bin/bash
cd /home/yourfolder
python -u scheduleMail.py

 

3. 如何杀死后台任务

这里有个坑,很多网上的博客没有说,我在这里提一下,以免大家重复去踩。

杀死该任务,就像杀死传统Linux进程一样

ps aux|grep scheduleMail
这里你会看到进程号,然后使用命令kill -9 scheduleMail就可以杀死该进程
 
但是,你会发现,进程虽然杀死了,后台任务仍在运行。
为什么呢?
你会你只是杀死了shell脚本的后台进程。
这里,你需要使用命令ps -e查看所有进程,
发现还有python进程在运行,杀死该python进程就好了
这样,整个后台任务就真的被杀死了!
 
目录
相关文章
|
3天前
|
消息中间件 安全 Linux
服务器(Linux)在线下载activeMQ以及配置打开
服务器(Linux)在线下载activeMQ以及配置打开
14 3
|
3天前
|
网络协议
​LabVIEW从另一个VI或通过VI服务器访问正在运行的可执行文件
​LabVIEW从另一个VI或通过VI服务器访问正在运行的可执行文件
13 0
|
1天前
|
Web App开发 Ubuntu Linux
Linux无图形界面环境使用Python+Selenium实践
【5月更文挑战第1天】Linux无图形界面环境使用Python+Selenium实践
15 2
|
2天前
|
Linux 网络安全
linux/服务器使用scp将一个服务器文件转移到另一个服务器上
linux/服务器使用scp将一个服务器文件转移到另一个服务器上
29 3
|
3天前
|
监控 JavaScript 网络协议
Linux系统之安装uptime-kuma服务器监控面板
【5月更文挑战第12天】Linux系统之安装uptime-kuma服务器监控面板
14 0
|
3天前
|
关系型数据库 MySQL Linux
服务器Linux系统配置mysql数据库主从自动备份
这是一个基本的配置主从复制和设置自动备份的指南。具体的配置细节和命令可能因您的环境和需求而有所不同,因此建议在操作前详细阅读MySQL文档和相关资源,并谨慎操作以避免数据丢失或不一致。
15 3
|
3天前
|
Oracle Java 关系型数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
【服务器】python通过JDBC连接到位于Linux远程服务器上的Oracle数据库
16 6
|
3天前
|
运维 监控 安全
2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
2023年最详细的:本地Linux服务器安装宝塔面板,并内网穿透实现公网远程登录
|
3天前
|
Linux 网络安全 数据库
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
linux centos系统搭建samba文件服务器 NetBIOS解析 (超详细)
|
3天前
|
域名解析 存储 缓存
Linux中搭建DNS 域名解析服务器(详细版)
Linux中搭建DNS 域名解析服务器(详细版)

热门文章

最新文章