搭建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 类或者函数,有时也会依赖于环境变量,所以记得环境变量的加载。
另外,脚本自启动后会发送邮件提醒,非常方便。