supervisor- python进程管理

安装

pip3 install supervisor

创建配置文件

sudo echo_supervisord_conf > /etc/supervisor/supervisord.conf

“ /etc/supervisord.conf”路径名称可以自定义设置

创建子进程配置文件路径
mkdir -p /etc/supervisor/conf.d/
修改配置文件
sudo vim /etc/supervisor/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/supervisor/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

image.png

重启 supervisord
supervisorctl reload
web 浏览

image.png

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时尚IT男

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值