全部
常见问题
产品动态
精选推荐

supervisor简介

管理 管理 编辑 删除

1、概述

supervisor是一个用python语言编写的进程管理工具,它可以很方便的监听、启动、停止、重启一个或多个进程。当一个进程意外被杀死,supervisor监听到进程死后,可以很方便的让进程自动恢复,不再需要程序员或系统管理员自己编写代码来控制。

2、架构:三大构成要素

  • supervisord

.supervisor的服务端:运行supervisor时会启动一个进程supervisord,它负责启动所管理的进程,并将所管理的进程作为自己的子进程来启动,而且可以在所管理的进程出现崩溃时自动重启

  • supervisorctl
    supervisor的客户端:supervisorctl是命令行管理工具,可以用命令来进行子进程的管理,supervisorctl常见命令

186da20230612144137810.png

  • echo_supervisord_conf
    默认的配置文件,一般生成默认文件为 supervisor.conf

3、安装

3.1、supervisor是基于python写的,所以使用pip来安装即可。
pip install supervisor
3.2、默认生成的几个地址需要我们关注

# supervisord 路径
/usr/local/bin/supervisord
# supervisorctl 路径
/usr/local/bin/supervisorctl
# echo_supervisord_conf 路径
/usr/local/bin/echo_supervisord_conf

如上路径,我们也可以通过whereis supervisordwhereis supervisorctlwhereis echo_supervisord_conf得到
3.3、验证是否安装成功
supervisorctl --help

4、配置

4.1、创建 /etc/supervisor 目录
mkdir /etc/supervisor

4.2、创建并修改supervisord.conf文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
vi /etc/supervisor/supervisord.conf

# 将unix_http_server 下的 file 路径改成如下内容
[unix_http_server] 
file=/var/run/supervisor.sock  ; (the path to the socket file)
# 将supervisord 下的logfile 路径 和 pidfile 路径改成如下内容
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file; default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile; default supervisord.pid)
[supervisorctl] 
serverurl=unix:///var/run/supervisor.sock ; (use a unix:// URL  for a unix socket)
# 将include 取消注释并将其下的 files 路径改成如下内容。标记着supervisor将会默认运行/etc/supervisor/conf.d的所有conf配置文件
[include]
files = /etc/supervisor/conf.d/*.conf

注:上面的路径只是推荐路径,你也可以根据自己的想法,指向不同路径
4.3、创建并添加文件权限(上文提到的)

# 创建文件
#touch /var/run/supervisor.sock  # 依据配置文件自动创建

mkdir /var/log/supervisor
touch /var/log/supervisor/supervisord.log

#touch /var/run/supervisord.pid  # 依据配置文件自动创建
mkdir /etc/supervisor/conf.d
# 添加权限
#chmod 777 /var/run 
#chmod 777 /var/log

4.4、配置supervisor开机自动启动服务(非必须,按需选择)
4.4.1、编辑文件(一般自带,不需要配置)
vim /usr/lib/systemd/system/supervisord.service
supervisord.service 文件内容如下:

[Unit] 
Description=Supervisor daemon 
[Service] 
Type=forking ExecStart=/usr/local/bin/supervisord -c /etc/supervisor/supervisord.conf 
ExecStop=/usr/local/bin/supervisorctl shutdown 
ExecReload=/usr/local/bin/supervisorctl reload 
KillMode=process Restart=on-failure 
RestartSec=42s 
[Install] 
WantedBy=multi-user.target

4.4.2、使能服务
systemctl enable supervisord
4.4.3、验证是否使能成功

  • 方法一,出现enable说明成功
    systemctl is-enabled supervisord
  • 方法二,开关机验证

注:supervisor常用命令

934a7202306121444027380.png

5、如何配置新服务并系统控制?

假设服务名称为test。启动文件为py类文件entry.py
5.1、创建test.conf并编辑配置文件vi /etc/supervisor/conf.d/test.conf

[program:test]  # 服务名称test
directory = /home/test_project        # 项目路径,项目运行前,会先切换到这个目录
command= /home/test_project/entry.py  # 程序入口主文件绝对路径 
autostart=true                        # 如果是true的话,子进程将在supervisord启动后被自动启动,默认就是true 
autorestart=true                      # 子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。false表示无论什么情况下,都不会被重新启动;unexpected表示只有当进程的退出码不在下面的exitcodes里面定义的退出码的时候,才会被自动重启。当为true的时候,只要子进程挂掉,将会被无条件的重启 
user=root                             # root用户执行 
redirect_stderr=true                  # 将stderr重定向stdout,默认false,与stderr_logfile互斥
startsecs = 5                         # 子进程启动多少秒之后,此时状态如果是running,我们认为启动成功了,默认值1
startretries=5                        # 当进程启动失败后,最大尝试的次数。当超过5次后,进程的状态变为FAIL
stdout_logfile = None                 # 正常日志输出文件,None表示不输出
stderr_logfile = None                 # 错误日志输出文件,None表示不输出

5.2、使用supervisorctl客户端查看程序启动的状态前需要先启动supervisor服务(使用supervisord)

supervisord -c /etc/supervisor/supervisord.conf  # 启动supervisor服务
# 如果出现
supervisorctl -c /etc/supervisor/supervisord.conf status  # 查看程序启动的状态

6、便捷

6.1、如上所示,下载supervisor后,启动项目需要以下几个步骤:

  1. 编辑supervisor配置文件,如supervisord.conf
  2. 创建文件夹及文件,如文件夹/var/log/supervisor、/etc/supervisor/conf.d,文件/var/run/supervisor.sock等
  3. 赋值权限,如chmod 777 /var/run
  4. 编辑程序配置文件,如/etc/supervisor/conf.d/test.conf
  5. 启动,如supervisord -c /etc/supervisor/supervisord.conf

而此时是有一个更好的方法,将①④合到一处。①中只关注重要的一些选项。④中照搬即可
vi /etc/supervisor/conf.d/test.conf

[unix_http_server]
file=/var/run/supervisor.sock  ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file; default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile; default supervisord.pid)
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; (use a unix:// URL  for a unix socket)
[include]
files = /etc/supervisor/conf.d/*.conf

[program:test]  # 服务名称test
directory = /home/test_project        # 项目路径,项目运行前,会先切换到这个目录
command= /home/test_project/entry.py  # 程序入口主文件绝对路径 
autostart=true                        # 如果是true的话,子进程将在supervisord启动后被自动启动,默认就是true 
autorestart=true                      # 子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。false表示无论什么情况下,都不会被重新启动;unexpected表示只有当进程的退出码不在下面的exitcodes里面定义的退出码的时候,才会被自动重启。当为true的时候,只要子进程挂掉,将会被无条件的重启 
user=root                             # root用户执行 
redirect_stderr=true                  # 将stderr重定向stdout,默认false,与stderr_logfile互斥
startsecs = 5                         # 子进程启动多少秒之后,此时状态如果是running,我们认为启动成功了,默认值1
startretries=5                        # 当进程启动失败后,最大尝试的次数。当超过5次后,进程的状态变为FAIL
stdout_logfile = None                 # 正常日志输出文件,None表示不输出
stderr_logfile = None                 # 错误日志输出文件,None表示不输出

6.2、此时需要做的步骤

  1. 编辑系统配置文件与程序配置文件,6.1中已配置完毕
  2. 创建文件夹及文件,参考上文
  3. 赋值权限,参考上文
  4. 启动,此时的启动命令就由原来的supervisord -c /etc/supervisor/supervisord.conf变为现在的supervisord -c /etc/supervisor/conf.d/test.conf


请登录后查看

CRMEB-慕白寒窗雪 最后编辑于2023-06-12 14:46:01

快捷回复
回复
回复
回复({{post_count}}) {{!is_user ? '我的回复' :'全部回复'}}
排序 默认正序 回复倒序 点赞倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level }}

作者 管理员 企业

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推荐': '推荐'}}
{{item.is_suggest == 1? '取消推荐': '推荐'}}
沙发 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暂无简介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打赏
已打赏¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回复' : '回复'}}
删除
回复
回复

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回复 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打赏
已打赏¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回复' : '回复'}}
删除
回复
回复
查看更多
打赏
已打赏¥{{reward_price}}
2323
{{like_count}}
{{collect_count}}
添加回复 ({{post_count}})

相关推荐

快速安全登录

使用微信扫码登录
{{item.label}} 加精
{{item.label}} {{item.label}} 板块推荐 常见问题 产品动态 精选推荐 首页头条 首页动态 首页推荐
取 消 确 定
回复
回复
问题:
问题自动获取的帖子内容,不准确时需要手动修改. [获取答案]
答案:
提交
bug 需求 取 消 确 定
打赏金额
当前余额:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
请输入 0.1-{{reward_max_price}} 范围内的数值
打赏成功
¥{{price}}
完成 确认打赏

微信登录/注册

切换手机号登录

{{ bind_phone ? '绑定手机' : '手机登录'}}

{{codeText}}
切换微信登录/注册
暂不绑定
CRMEB客服

CRMEB咨询热线 咨询热线

400-8888-794

微信扫码咨询

CRMEB开源商城下载 源码下载 CRMEB帮助文档 帮助文档
返回顶部 返回顶部
CRMEB客服