supervisor- python进程管理
2022-06-20 本文已影响0人
时尚灬IT男
安装
pip3 install supervisor
创建配置文件
sudo echo_supervisord_conf > /etc/supervisord.conf
“ /etc/supervisord.conf”路径名称可以自定义设置
不在etc路径下,可能导致supervisorctl命令只有在配置文件所在目录才生效。
创建子进程配置文件路径
mkdir -p /etc/supervisor/conf.d/
修改配置文件
sudo vim /etc/supervisord.conf
将最后一部分改为
[include]
files = /etc/supervisor/conf.d/*.conf
打开web 浏览
[inet_http_server] ; inet (TCP) server disabled by default
port=*:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
*注意
每行指令前的“;”删除了再生效
启动 supervisord
supervisord -c /etc/supervisord.conf
查看版本
supervisord -v
查看进程状态(还没配程序,此时空输出)
supervisorctl status
编写简单的 python 脚本test.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
json配置文件类,调用方法
data_dict = {"a":"1", "b":"2"}
JsonConf.set(data_dict)
即可在当前目录下生成json文件:config.json
'''
import time
from loguru import logger
import json
import os,sys
config_path = "config.json"
class JsonConf:
'''json配置文件类'''
# 'C:\wb\PycharmProjects\数据同步-解析\server_config.json'
@staticmethod
def store(data):
with open(config_path, 'w') as json_file:
json_file.write(json.dumps(data, indent=4))
@staticmethod
def load():
if not os.path.exists(config_path):
with open(config_path, 'w') as json_file:
pass
with open(config_path) as json_file:
try:
data = json.load(json_file)
except Exception as e:
# print(e)
data = {}
return data
@staticmethod
def set(data_dict):
json_obj = JsonConf.load()
for key in data_dict:
json_obj[key] = data_dict[key]
JsonConf.store(json_obj)
json.dumps(json_obj, indent=4)
if __name__ == "__main__":
logger.remove(handler_id=None)
logger.add( "log/log_{time}.log", enqueue=True, rotation='00:00',
retention='5 days')
logger.info("开始运行")
data = {"a": "1", "f": "100", "b": "3000"}
JsonConf.set(data)
print(JsonConf.load())
i = 1
while True:
time.sleep(5)
i += 1
data = {"a": "{}".format(i), "f": "100", "b": "3000"}
JsonConf.set(data)
print(JsonConf.load())
logger.info(JsonConf.load())
创建子进程配置文件
vim /etc/supervisor/conf.d/test.conf
内容如下
[program:test3] ; 项目名
directory=/home/Desktop/supervisor/test3/ ;脚本执行路径
command=python3 /home/Desktop/supervisor/test2/test.py ; 脚本执行命令
priority=999 ; 相对启动优先级,数值越小越优先,默认为999
autostart=true ; 在supervisor启动时自动启动,默认为true
autorestart=true ; 在意外退出时重新启动,默认为true
startsecs=10 ; 子进程启动多少秒后状态为running则认为启动成功,默认为1
startretries=3 ; 尝试启动的最大次数,默认为3
exitcodes=0,2 ; 进程的预期退出代码列表,默认为0
stopsignal=QUIT ; 终止进程的信号,默认为TERM
stopwaitsecs=10 ; 在SIGKILL之前等待的最大秒数,默认为10
; user=root ; 在某用户下设置uid来启动程序,默认不切换用户
redirect_stderr=true ; 是否重定向stdout和stderr,默认为false
stdout_logfile=/home/parallels/Desktop/supervisor/test3/test3.log ; stdout的输出文件,默认为AUTO
stdout_logfile_maxbytes=50MB ; stdout最大文件大小,默认为50MB
stdout_logfile_backups=10 ; stdout文件备份数,设为0则不备份,默认为10
*注意如上配置信息:
directory=/home/Desktop/supervisor/test3/ ;脚本执行路径
command=python3 /home/Desktop/supervisor/test2/test.py 是python代码文件路径
重新读去配置并更新子进程:
supervisorctl reread
supervisorctl update
查看进程状态
supervisorctl status

重启 supervisord
supervisorctl reload
web 浏览

supervisorctl 命令
改动某配置文件,重载
supervisorctl update
新增某配置文件,重载
supervisorctl reread
重启 supervisord
supervisorctl reload
查看所有进程状态
supervisorctl status
查看指定进程状态
supervisorctl status <name>
启动所有子进程
supervisorctl start all
启动指定子进程
supervisorctl start <name>
重启所有子进程
supervisorctl restart all
重启指定子进程
supervisorctl restart <name>
停止所有子进程
supervisorctl stop all
停止指定子进程
supervisorctl stop <name>
添加子进程到进程组
supervisorctl add <name>
从进程组移除子进程,需要先stop。注意:移除后,需要使用reread和update才能重新运行该进程
supervisorctl reomve <name>