supervisor篇---后台程序监控

supervisor 官网 http://supervisord.org/installing.html

一:安装

安装通过 apt-get 安装 (最好直接用root权限安装)

sudo apt-get install supervisor

二:配置

echo_supervisord_conf > /etc/supervisord.conf

这行命令可以直接生成 supervisord.conf 的配置文件, 但是我不太建议用这个,会有很多问题

files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini
注意: conf 或 ini 只能选择其中一种,(如果有多个配置文件的情况下), 并且要将 ;(分号)去掉

本人的 /etc/supervisord.conf

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[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

; 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 = /etc/supervisor/conf.d/*.conf
files = /etc/supervisor/conf.d/*.ini

或者 配置文件二: 直接拷贝以下的配置文件,可以直接使用

[unix_http_server]
file=/run/supervisor/supervisor.sock   ; (the path to the socket file)

[supervisord]
logfile=/var/log/supervisor/supervisord.log  ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB       ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10          ; (num of main logfile rotation backups;default 10)
loglevel=info               ; (log level;default info; others: debug,warn,trace)
pidfile=/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false              ; (start in foreground if true;default false)
minfds=1024                 ; (min. avail startup file descriptors;default 1024)
minprocs=200                ; (min. avail process descriptors;default 200)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket

[include]
files = supervisor.d/*.conf  # 路径可以按自己的需求放置

三:程序配置

本人的程序配置

1.首先,修改配置文件,主要是修改 program 配置的位置, 一般都是放在 /etc下

[include]
files = /etc/supervisor/conf.d/*.conf

2.通过 vi 创建文件

/etc/supervisor/conf.d/app.conf

3.接下来是 program 的配置

[program:app01]
directory = /home/efc/app/object-detections
command = python3 /home/efc/app/object-detections/app_test_time.py
autostart = true ; 在 supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数,默认是 3
user = root ; 用哪个用户启动
redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
stdout_logfile = /home/efc/app/object-detections/app.log ;日志文件

重要的事情说三遍!!!
重要的事情说三遍!!!
重要的事情说三遍!!!
##########################################################################################
注意: user = root 这个可以更改,根据具体的环境选择不同的用户,不是所有的都是用root权限的

本人是通过 python3 来启动程序的

四:启动 superviosr

very import 的几句话

supervisord -c /etc/supervisor/supervisord.conf

supervisorctl update
supervisorctl status

五:supervisorctl使用

supervisorctl是supervisord的命令行客户端管理工具,用来管理进程。

注意:使用命令要在外层路径下
在这里插入图片描述
读取配置,进入交互shell环境

supervisord -c /etc/supervisor/conf.d/app.conf

supervisorctl
#读取配置文件
reread
#启动程序 
start app01

在这里插入图片描述

命令说明 (非进入交互模式)

supervisorctl status        //查看所有进程的状态
supervisorctl update        //配置文件修改后使用该命令加载新的配置

supervisorctl stop app01       //停止app01
supervisorctl start app01     //启动app01
supervisorctl restart       //重启app01
supervisorctl reload        //重新启动配置中的所有程序

important

1、启动服务

systemctl start supervisord

2、查看服务启动状态

systemctl status supervisord

在这里插入图片描述

3、查看是否存在supervisord进程

ps -ef |grep supervisord

在这里插入图片描述4、停止服务

方法一:

supervisorctl shutdown

方法二:

systemctl stop supervisord

5、重新载入配置信息

systemctl reload supervisord

6、重启服务

systemctl restart supervisord

六:设置开机自启

ubuntu

1.编辑 /etc/rc.local

vi /etc/ec.local

2.在 exit 0 之前加入以下命令

/usr/local/bin/supervisord

保存退出

3.最后修改 rc.local 的权限

chmod +x /etc/rc.local

centos

1.新建文件 supervisord.service

#supervisord.service

[Unit] 
Description=Supervisor daemon

[Service] 
Type=forking 
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf 
ExecStop=/usr/bin/supervisorctl shutdown 
ExecReload=/usr/bin/supervisorctl reload 
KillMode=process 
Restart=on-failure 
RestartSec=42s

[Install] 
WantedBy=multi-user.target

2.将文件拷贝到 /usr/lib/systemd/system/

cp supervisord.service /usr/lib/systemd/system/

3.启动服务

systemctl enable supervisord

4.验证一下是否为开机启动

systemctl is-enabled supervisord
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

心惠天意

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

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

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

打赏作者

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

抵扣说明:

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

余额充值