supervisor

原文连接:https://www.jianshu.com/p/ac6c84a2f415
原文连接:https://blog.csdn.net/zou79189747/article/details/80403016
原文连接:https://www.jianshu.com/p/0b9054b33db3

官网:http://supervisord.org/
github:https://github.com/Supervisor/supervisor

Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。

安装指令pip install supervisor
在这里插入图片描述
生成默认配置文件/etc/supervisor/supervisord.conf
官网是直接生成的,指令如下:

mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf

但是由于我没有服务器的root权限,需要先到用户目录下面生成,然后再移动过去。

mkdir /etc/supervisor
sudo echo_supervisord_conf > /home/qhdata/supervisord.conf
sudo mv  /home/qhdata/supervisord.conf    /etc/supervisor/supervisord.conf

然后修改supervisord.conf文件
ps:要开启某个服务的话,服务上面的中括号的服务名称也要一并关闭注册。
修改需要监控进程位置:
原来的
在这里插入图片描述
修改成拥有权限的位置/home/qhdata/supervisord/*.ini
在这里插入图片描述
启动http服务
在这里插入图片描述

username跟password注释时为免密登陆。
sock目录-记录程序信息等
在这里插入图片描述
在这里插入图片描述

日志输出目录,pid文件目录
在这里插入图片描述
其他的修改按需来设置。
创建文件夹
mkdir /home/qhdata/supervisord/
mkdir /home/qhdata/supervisord/log/
mkdir /home/qhdata/tmp/
mkdir /home/qhdata/supervisord/tmp/

启动 服务端
sudo supervisord -c /etc/supervisor/supervisord.conf指定配置文件目录

关闭服务端

ps -ef | grep supervisord
sudo kill -s SIGTERM [pid]

supervisord指令块
原文:http://supervisord.org/running.html

class SupervisordCommand():
    @staticmethod
    def execute(command):
        output = subprocess.Popen(command,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  shell=True)  # 基于win的调度
        stdoutdata, stderrdata = output.communicate()  # 放内存
        return stdoutdata, stderrdata

    @staticmethod
    def start():
        # 启动服务端
        command = "sudo supervisord"
        stdoutdata, stderrdata  = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    # @staticmethod
    # def close():
    #     # 关闭服务
    #     command = "ps -ef | grep supervisord"
    #     stdoutdata, stderrdata  = SupervisordCommand.execute(command)
    #     pid = stdoutdata
    #     command = f"sudo kill -s SIGTERM {pid}"
    #     stdoutdata, stderrdata = SupervisordCommand.execute(command)
    #     return stdoutdata, stderrdata
    @staticmethod
    def close():
        # 关闭服务
        command = f"sudo supervisorctl shutdown"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata


    @staticmethod
    def reload():
        #  重启远程监控
        command = f"sudo supervisorctl reload"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def status():
        # 查看状态
        command = "sudo supervisorctl status"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def update():
        # 重新加载配置并根据需要添加/删除,并将重新启动受影响的程序
        command = "sudo supervisorctl update"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def updateAll():
        # 重新加载配置并根据需要添加/删除,并将重新启动受影响的程序
        command = "sudo supervisorctl update all"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def clear(program_name):
        # 清除进程的日志文件。
        command = f"sudo supervisorctl clear {program_name}"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def pid(program_name):
        # 按名称获取单个子进程的 PID。
        command = f"sudo supervisorctl pid {program_name}"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def pidAll():
        # 获取每个子进程的 PID,每行一个。
        command = "sudo supervisorctl pid all"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata



    @staticmethod
    def stopProgram(program_name):
        # 停止某个进程
        command = f"sudo supervisorctl stop {program_name}"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def stopAll():
        # 关闭所有进程
        command = f"sudo supervisorctl stop all"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def startProgram(program_name):
        # 启动某个进程, 不停supervisor 添加program
        command = f"sudo supervisorctl start {program_name}"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def startAll():
        # 启动所有进程
        command = f"sudo supervisorctl start all"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def restartProgram(program_name):
        # 重启某个进程
        command = f"sudo supervisorctl restart {program_name}"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def restarAll():
        # 重新启动所有进程 注意:重新启动不会重新读取配置文件。为此,请参阅重读和更新。
        command = f"sudo supervisorctl restart all"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def tailProgramStdout(program_name):
        # 输出进程日志的最后一部分 例如: tail -f <name> 命名进程的连续尾部 stdout Ctrl-C 退出。
        # tail -100 <name>进程 stdout 的最后 100个字节tail <name> stderr进程 stderr 的最后 1600个字节
        command = f"sudo supervisorctl tail {program_name} stdout"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def tailProgramStderr(program_name):
        # 输出进程日志的最后一部分 例如: tail -f <name> 命名进程的连续尾部 stdout Ctrl-C 退出。
        # tail -100 <name>进程 stdout 的最后 100个字节tail <name> stderr进程 stderr 的最后 1600个字节
        command = f"sudo supervisorctl tail {program_name} stderr"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

    @staticmethod
    def programConfig(file_name):
        # 查询配置文件
        command = f"cat {supervisor_process_path}/{file_name}.ini"
        stdoutdata, stderrdata = SupervisordCommand.execute(command)
        return stdoutdata, stderrdata

20220722补充

监控进程配置文件

[program:manager-system__api_server]
command = sudo -S password python3 -m uvicorn main:app --host 0.0.0.0 --port 8000 --no-access-log
directory = /data/cn/qhdata/python/git/projects_etl/projects_etl/api_servers/api_server/
autostart = true
autorestart = true
startretries = 3
user = qhdata
priority = 1
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 0
stdout_logfile = /home/qhdata/supervisord/log/manager-system__api_server.log

20230213记录

参考文章:https://cloudwafer.com/blog/how-to-install-and-configure-supervisor-on-centos-7/
参考文章:https://stackoverflow.com/questions/45224707/install-and-configure-supervisord-on-centos-7-to-run-laravel-queues-permanently
在给一台本地服务器安装时,出现启动失败问题。服务器版本如下:

[root@localhost projects_etl]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

问题情况如下:
安装情况
在这里插入图片描述
启动没有报错
在这里插入图片描述
查看status没有返回任何信息
在这里插入图片描述
http服务没有启动

[root@localhost ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:55055           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN   

端口无服务。
参考文章使用yum重新安装。
修改配置信息

sudo vim /usr/lib/systemd/system/supervisord.service

修改前

[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target

修改后

[Unit]
Description=Supervisor process control system for UNIX
Documentation=http://supervisord.org
After=network.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf

[Install]
WantedBy=multi-user.target

但是启动时出错

[root@localhost projects_etl]# sudo systemctl start supervisord
Failed to start supervisord.service: Unit not found.

不知道那里出现问题。可能是本身服务器出现问题。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值