在虚拟机中安装Linux中的CentOS7系统
https://baijiahao.baidu.com/s?id=1597320700700593557&wfr=spider&for=pc
因为安装的centos7的python版本为2.7我们要将他的版本升为3.5
安装依赖
shell> yum -y install zlib-devel \
bzip2-devel openssl-devel ncurses-devel \
sqlite-devel readline-devel tk-devel \
gdbm-devel db4-devel libpcap-devel xz-devel
下载编译安装
shell> cd /usr/local/src/
shell> wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
shell> tar xf Python-3.6.5.tgz
shell> cd Python-3.6.5
shell> ./configure --prefix=/usr/local/python3.6.5
shell> make -j 4 && make install
shell> ln -s /usr/local/python3.6.5 /usr/local/python3
shell> echo -e '# Python PATH\nexport PATH=/usr/local/python3/bin:$PATH' >>/etc/profile
shell> source /etc/profile
shell> python3 -m pip install --upgrade pip
写一个在linux系统中持续运行的后台程序
import time
def sleeptime(hour,min,second):
return hour * 3600 + min * 60 + second
a =1
sec=sleeptime(0, 0, 5)
while a == 1:
time.sleep(sec)
print('kaishizhixing')
每隔5秒输出一次 也可以直接写一个跟简单的
a = 1
while a =1:
print('kaishizhixing")
写一个监控脚本 检测程序是否一致在执行
我们可以在shell命令中查询程序的状态
ps -ef|grep jiaoben.py
15589和13784是该进程的主id和副id
编写一个监控进程,监控此脚本是否一直运行。
import os
import re
import datetime
import paramiko
import smtplib
from email.mime.text import MIMEText
from email.header import Header
process ="/home/languid/jiaoben.lock"
os.system("ps -ef|grep jiaoben.py|grep -v grep >%s" % process)
if not(os.path.getsize(process)):
os.system("nohup /home/languid/jiaoben.py %")
baocuoemali()
else:
print("脚本正常!")
再增加一个当脚本发生错误时的邮件通知。
#!/usr/bin/python3
import smtplib
from email.mime.text import MIMEText
from email.header import Header
sender = 'from@baocuo.com'
receivers = ['626549364@qq.com']
message = MIMEText('脚本错误...', 'plain', 'utf-8')
message['From'] = Header("languid", 'utf-8')
message['To'] = Header("测试", 'utf-8')
subject = 'Python SMTP 报错'
message['Subject'] = Header(subject, 'utf-8')
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message.as_string())
print ("邮件发送成功")
except smtplib.SMTPException:
print ("Error: 无法发送邮件")
来源:oschina
链接:https://my.oschina.net/u/4392265/blog/3565454