python脚本自启动和定时启动

搭建IP代理池的时候,我发现这些拨号VPS并不稳定,拨号次数多了需要重启才能继续拨号。这时候需要定时或者按需重启系统并启动代理脚本。

定时可以用crontab,但是脚本自启动用crontab有不便之处:设置环境变量等不太方便。最终我用crontab定时重启,Python脚本自启动用了service,即在/etc/init.d/新建一个service.sh,

# coding=utf-8
#!/bin/bash
export PATH="$PATH:/root/anaconda3/bin:/root/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
nohup /root/anaconda3/bin/python /root/anaconda3/lib/python3.8/site-packages/adslproxy/sender/sender.py >> /root/proxy_reboot.log 2>&1 &
# 脚本自启动需要加载环境变量,因为脚本依赖一些环境变量,然后rc.local里写入bash service.sh即可,系统启动时service.sh会自启动
# crontab:
*/10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org | logger -t NTP
@reboot adsl-start
#@reboot export PATH="$PATH:/root/anaconda3/bin:/root/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
*/60 * * * * /root/anaconda3/bin/python /root/anaconda3/lib/python3.8/site-packages/adslproxy/sender/reboot.py >> /root/reboot.log 2>&1 &
#@reboot nohup /root/anaconda3/bin/python /root/anaconda3/lib/python3.8/site-packages/adslproxy/sender/sender.py >> /root/proxy_reboot.log 2>&1 &

脚本自启动最关键的是环境变量的加载,很多时候我们的脚本会从另外一些.py import 类或者函数,有时也会依赖于环境变量,所以记得环境变量的加载。

另外,脚本自启动后会发送邮件提醒,非常方便。

python邮件提醒

python脚本邮件提醒实现简单监控

简单粗暴,直接上代码。email通知在某些应用中还是必需的,当然也可以微信推送。

# -*- coding: utf-8 -*-
import time
# from playsound import playsound
from datetime import datetime
import smtplib
from email.mime.text import MIMEText
from email.header import Header

class EmailClient(object):
    def __init__(self):
        """
        初始化邮件列表     
                 
        """
        self.to_list = ["receiver_email"]
    def notification(self, body, subj):
        sender = 'sender_email'  # 邮件发送人
        receiver = 'receiver_email'  # 邮件收件人
        subject = 'adslproxy error: ' + subj + ' ' + str(datetime.today())[:16]  # 主题
        smtpserver = 'smtp.163.com'  # 网易的STMP地址 默认端口号为25
        username = 'sender_email'  # 发送邮件的人
        password = EMAIPASS  # 非网易登录密码.网易在开通SMTP服务后会有个密码设置

        # 中文需参数‘utf-8',单字节字符不需要
        msg = MIMEText(body, 'plain', 'utf-8')
        msg['Subject'] = Header(subject, 'utf-8')  # 头部信息:标题
        msg['From'] = 'user<sender_email>'  # 头部信息:名称<发件人的地址>
        msg['To'] = ",".join(self.to_list)  # 头部信息:收件人地址
        m = True
        while m == True:
            try:        
                smtp = smtplib.SMTP_SSL('smtp.163.com', 465)
                smtp.login(username, password)
                smtp.sendmail(sender, receiver, msg.as_string())
                smtp.quit()
                print('success')
                m = False
            except smtplib.SMTPException as e:
                print('Error: ', e)
                time.sleep(25)

emailclient = EmailClient()
emailclient.notification('test email', 'proxy')

在其他程序中调用时直接Import这个类即可:
from adslproxy.sendemail import EmailClient
比如:
    try:
        emailclient = EmailClient()
        emailclient.notification('adslproxy started', 'ygg22457 adslproxy started')
        print('email test success')
    except Exception as e:
        print(e)

本来还想用下微信推送的,后来想想太容易被封号,算了吧。