美文网首页
python 调用qq邮箱 linux 执行每天自动发送邮件

python 调用qq邮箱 linux 执行每天自动发送邮件

作者: a十二_4765 | 来源:发表于2017-11-07 10:38 被阅读72次

    定时任务参考文档定时任务

    python 代码

    #!/usr/bin/python

    # -*- coding: UTF-8 -*-

    import smtplib

    from email.mime.text import MIMEText

    _user = "1158219108@qq.com"  #服务器邮箱账号

    _pwd  = "***********"  #密钥

    _to  = "***********"  # 接收人账号

    msg = MIMEText("hi")  #接受内容

    msg["Subject"] = "hi " # 标题头

    msg["From"]    = _user

    msg["To"]      = _to

    try:

    s = smtplib.SMTP_SSL("smtp.qq.com", 465) # qq邮箱服务器

    s.login(_user, _pwd)

    s.sendmail(_user, _to, msg.as_string())

    s.quit()

    print ("Success!")

    except smtplib.SMTPException.e:

    print ("Falied,%s"%e)

    linux 定时任务

    1.安装 yum install crontabs

    2. crontab -e  打开编辑器进行查看

    00  7 * * * python /run/youjian.py

    代表每天早上七点执行python 代码

    然后esc :wq 退出

    crontab -l 查看

    实例

    实例1:每1分钟执行一次command

    命令:

    * * * * * command

    实例2:每小时的第3和第15分钟执行

    命令:

    3,15 * * * * command

    实例3:在上午8点到11点的第3和第15分钟执行

    命令:

    3,15 8-11 * * * command

    实例4:每隔两天的上午8点到11点的第3和第15分钟执行

    命令:

    3,15 8-11 */2 * * command

    实例5:每个星期一的上午8点到11点的第3和第15分钟执行

    命令:

    3,15 8-11 * * 1 command

    实例6:每晚的21:30重启smb

    命令:

    30 21 * * * /etc/init.d/smb restart

    实例7:每月1、10、22日的4 : 45重启smb

    命令:

    45 4 1,10,22 * * /etc/init.d/smb restart

    实例8:每周六、周日的1 : 10重启smb

    命令:

    10 1 * * 6,0 /etc/init.d/smb restart

    实例9:每天18 : 00至23 : 00之间每隔30分钟重启smb

    命令:

    0,30 18-23 * * * /etc/init.d/smb restart

    实例10:每星期六的晚上11 : 00 pm重启smb

    命令:

    0 23 * * 6 /etc/init.d/smb restart

    实例11:每一小时重启smb

    命令:

    * */1 * * * /etc/init.d/smb restart

    实例12:晚上11点到早上7点之间,每隔一小时重启smb

    命令:

    * 23-7/1 * * * /etc/init.d/smb restart

    实例13每月的4号与每周一到周三的11点重启smb

    命令:

    0 11 4 * mon-wed /etc/init.d/smb restart

    实例14:一月一号的4点重启smb

    命令:

    0 4 1 jan * /etc/init.d/smb restart

    实例15:每小时执行/etc/cron.hourly目录内的脚本

    命令:

    01   *   *   *   *     root run-parts /etc/cron.hourly

    相关文章

      网友评论

          本文标题:python 调用qq邮箱 linux 执行每天自动发送邮件

          本文链接:https://www.haomeiwen.com/subject/namdmxtx.html