让别人不能关掉python程序_python程序的守护程序supervisor,让你的python永不停止...

supervisor是python程序部署中常用的守护程序,可以用于网络爬虫/python开发的web程序/定时程序······的守护,使其在未知bug/意外出错停止的情况下重启,supervisor+nginx常作为web的部署方式。

下面介绍完整的使用流程:安装supervisor

pip install supervisor配置

运行echo_supervisord_conf命令输出默认的配置项,可以如下操作将默认配置保存到文件中

echo_supervisord_conf > supervisord.conf

文件内容最重要的是最后两行

The [include] section can just contain the "files" setting. This

; setting can list multiple files (separated by whitespace or

; newlines). It can also contain wildcards. The filenames are

; interpreted as relative to this file. Included files *cannot*

; include files themselves.

;[include]

;files = relative/directory/*.ini

最后两行需要取消注释,作用是指出supervisor要执行的守护脚本的文件夹,该文件夹下符合文件命名的配置文件都会被执行。

如:

[include]

files = /etc/supervisor/*.conf

意思是指明/etc/supervisor文件里的以.conf为结尾的配置文件都要执行。

然后就是在etc下创建supervisor文件,用户存放配置文件

编辑配置文件

其他一些配置字段

priority=1 ;数字越高,优先级越高

numprocs=1 ; 启动几个进程

autostart=true ; 随着supervisord的启动而启动

startretries=10 ; 启动失败时的最多重试次数

exitcodes=0 ; 正常退出代码(是说退出代码是这个时就不再重启了吗?待确定)

stopsignal=KILL ; 用来杀死进程的信号

stopwaitsecs=10 ; 发送SIGKILL前的等待时间

redirect_stderr=true ; 重定向stderr到stdout

启动

supervisord -c /etc/supervisord.conf查看 supervisord 是否在运行:

ps aux | grep supervisordsupervisorctl 是supervisord的客户端

我们可以利用supervisorctl来管理supervisor。

supervisorctl

> status # 查看程序状态

> stop tornadoes:* # 关闭 tornadoes组 程序

> start tornadoes:* # 启动 tornadoes组 程序

> restart tornadoes:* # 重启 tornadoes组 程序

> update # 重启配置文件修改过的程序

执行status命令时,显示如下信息说明tornado程序运行正常:

supervisor> status

tornadoes:tornado-8000 RUNNING pid 32091, uptime 00:00:02

tornadoes:tornado-8001 RUNNING pid 32092, uptime 00:00:02

tornadoes:tornado-8002 RUNNING pid 32093, uptime 00:00:02

tornadoes:tornado-8003 RUNNING pid 32094, uptime 00:00:02

重启supervisord

supervisorctl reload

ID:Python之战

|作|者|公(zhong)号:python之战

专注Python,专注于网络爬虫、RPA的学习-践行-总结

喜欢研究和分享技术瓶颈,欢迎关注

独学而无友,则孤陋而寡闻!

---------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值