编号 | 分类 | 文章及链接 | 介绍 | 作者 | 来源分类 | 撰写日期 | 收录日期 |
---|---|---|---|---|---|---|---|
S1 | 系统 | supervisor官网 | 官方手册 | supervisor | 2016-11-18 | 2022-12-04 | |
T1 | 技巧 | Supervisor使用详解 | 简书 | 2019-02-28 | 2022-12-05 | ||
T2 | 技巧 | Supervisor安装、配置 | garvenc | 博客园 | 2020-04-16 | 2020-08-09 | |
T3 | 技巧 | 进程管理supervisor的简单说明 | jyzhou | 博客园 | 2016-11-18 | 2022-12-04 |
nohup 及&的配合,可以非常简便地让某个程序在后台运行。supervisor则更进一步,不但可以让程序在后台运行,还可以在程序宕机后自动拉起,还可以指定启动用户,是提供不间断服务的优秀选项。
一、安装
安装
# 1:pip 安装(官方推荐安装方式):
pip install supervisor
# 2:Debian / Ubuntu可以直接通过apt安装:
apt-get install supervisor
配置开机启动
sudo systemctl enable supervisor
二、配置
1)通过apt-get install安装后,supervisor的配置文件在:
/etc/supervisor/supervisord.conf
supervisor的配置文件默认是不全的,不过在大部分默认的情况下,上面说的基本功能已经满足。而其管理的子进程配置文件在:
/etc/supervisor/conf.d/*.conf
然后,开始给自己需要的脚本程序编写一个子进程配置文件,让supervisor来管理它,放在/etc/supervisor/conf.d/目录下,以.conf作为扩展名(每个进程的配置文件都可以单独分拆也可以把相关的脚本放一起)。如任意定义一个和脚本相关的项目名称的选项组(/etc/supervisor/conf.d/test.conf):
配置管理子程序
下面是样本
[program:MFMVol]
#脚本目录
directory=/home/nftdigger/freeMinter
#脚本执行命令
command=/home/nftdigger/freeMinter/freeMinter /home/nftdigger/freeMinter/cpVol0731_M.conf
#supervisor启动的时候是否随着同时启动,默认True
autostart=true
#当程序exit的时候,这个program不会自动重启,默认unexpected
#设置子进程挂掉后自动重启的情况,有三个选项,false,unexpected和true。如果为false的时候,无论什么情况下,都不会被重新启动,如果为unexpected,只有>
autorestart=unexpected
#这个选项是子进程启动多少秒之后,此时状态如果是running,则我们认为启动成功了。默认值为1
startsecs=2
#日志输出
#stderr_logfile=/home/nftdigger/freeMinter/freeMinterVol_stderr.log
stderr_logfile=/home/nftdigger/freeMinter/fm_supervisor_err.log
#脚本运行的用户身份
user = nftdigger
#把 stderr 重定向到 stdout,默认 false
#redirect_stderr = true
#stdout 日志文件大小,默认 50MB
#stdout_logfile_maxbytes = 0
#stdout 日志文件备份数
#stdout_logfile_backups = 10
三、启动等控制命令
1. 服务控制命令
sudo systemctl start supervisor
sudo systemctl restart supervisor
sudo systemctl reload supervisor
sudo systemctl stop supervisor
2. 子进程控制命令
命令及参数 | 解说 |
---|---|
supervisorctl | 进入命令行交互界面,可直接输入子命令进行管理。 |
supervisorctl reload | 重新加载配置文件,并重启supervisord及所有自动启动的程序。 |
supervisorctl restart <name> | all | 重启程序,all表示重启所有程序。单个控制时可以写多个控制程序名。 |
supervisorctl start <name> | all | 启动程序,all表示启动所有程序。 |
supervisorctl status <name> | all | 查看程序状态,如为all或不指定则查看所有程序状态。 |
supervisorctl stop <name> | all | 停止程序,all表示停止所有程序。 |
supervisorctl update | 重新加载配置文件,并启动新配置指定的程序。 |
如果登录用户不是root,注意加上sudo获取root权限
命令例子:
sudo supervisorctl stop MFMVol
sudo supervisorctl start MFMVol
sudo supervisorctl restart MFMVol
四、附录
启动出错问题查找
1. supervisor状态出错
通过systemctl status supervisor可以监视服务是否正常开启。如果像下面这样,inactive,启动有问题,需要查看服务启动日志。
nftdigger@nftdigger-Ubuntu:/etc/supervisor$ sudo systemctl status supervisor
● supervisor.service - Supervisor process control system for UNIX
Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Mon 2022-12-05 01:02:47 CST; 9s ago
Docs: http://supervisord.org
Process: 2374076 ExecStart=/usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf (code=exited, status=2)
Main PID: 2374076 (code=exited, status=2)
查看服务启动日志命令:
journalctl -u supervisor.service
-- Logs begin at Tue 2022-10-11 09:44:26 CST, end at Mon 2022-12-05 09:35:02 CST. --
12月 04 21:31:18 nftdigger-Ubuntu systemd[1]: Started Supervisor process control system for UNIX.
12月 04 21:31:18 nftdigger-Ubuntu supervisord[2332429]: 2022-12-04 21:31:18,603 CRIT Supervisor is running as root. Privileges were not dropped>
12月 04 21:31:18 nftdigger-Ubuntu supervisord[2332429]: 2022-12-04 21:31:18,603 WARN No file matches via include "/etc/supervisor/conf.d/*.conf"
12月 04 21:31:18 nftdigger-Ubuntu supervisord[2332429]: 2022-12-04 21:31:18,605 INFO RPC interface 'supervisor' initialized
12月 04 21:31:18 nftdigger-Ubuntu supervisord[2332429]: 2022-12-04 21:31:18,605 CRIT Server 'unix_http_server' running without any HTTP authenti>
12月 04 21:31:18 nftdigger-Ubuntu supervisord[2332429]: 2022-12-04 21:31:18,606 INFO supervisord started with pid 2332429
12月 05 00:59:01 nftdigger-Ubuntu systemd[1]: Reloading Supervisor process control system for UNIX.
12月 05 00:59:01 nftdigger-Ubuntu supervisorctl[2373420]: Restarted supervisord
12月 05 00:59:01 nftdigger-Ubuntu supervisord[2332429]: Error: invalid literal for int() with base 10: '20m' in section 'program:MeiFMVol' (file>
12月 05 00:59:01 nftdigger-Ubuntu supervisord[2332429]: For help, use /usr/bin/supervisord -h
12月 05 00:59:01 nftdigger-Ubuntu systemd[1]: Reloaded Supervisor process control system for UNIX.
12月 05 00:59:01 nftdigger-Ubuntu systemd[1]: supervisor.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
12月 05 00:59:01 nftdigger-Ubuntu systemd[1]: supervisor.service: Failed with result 'exit-code'.
一看日志,好家伙,从网友哪里拷贝的配置,日志文件大小限制的20M这个是错误的!
#stdout 日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20M
看了supervisor官方手册(链接见本文头部),明白了这个单位有问题,没有M这个单位,应该是MB。