Linux配置Supervisor
Supervisor相关命令
# 启动supervisor
supervisord
# 通过配置文件启动supervisor
supervisord -c supervisor.conf
## -n 前台运行
## -c 指定配置
supervisord -n -c supervisor.conf
启动服务
# 启动所有/指定的程序进程
supervisorctl start [all]|[指定app名]
## 启动指定app
supervisorctl start test
# 关闭所有/指定的程序进程
supervisorctl stop [all]|[指定app名]
适用场景:
新增或修改了某个程序的配置(如添加/删除 [program:xxx] 块)。
需应用新配置但不重启现有进程。
# 重新读取配置文件
supervisorctl reread
# 应用新配置(自动启动新增程序,停止已移除的程序)
supervisorctl update
适用场景:
修改了 supervisord.conf 主配置文件(如日志路径、端口等全局设置)。
需重启所有子进程(可能导致服务短暂中断)
# 重载supervisor
supervisorctl reload
# 查看状态
supervisorctl status
# 查看日志
supervisorctl tail -f ${服务名}
# stderr 显示 halo 服务的标准错误日志
supervisorctl tail -f ${服务名} stderr
Ubuntu配置Supervisor
Ubuntu安装Supervisor
使用supervisor设置脚本开机自启
# Ubuntu安装Supervisor
apt install -y supervisor
# supervisor开机自启:
systemctl enable --now supervisor
app启动项配置详解:
参考地址: https://cikeblog.com/supervisor.html
管理应用的配置
进入到/etc/supervisor/conf.d
目录,创建管理应用的配置,可以创建多个应用配置。
# 创建app.conf配置
vim /etc/supervisor/conf.d/app.conf
配置内容如下:
[supervusird]
# 禁止后台运行
nodaemon=true
# 程序的名字,在supervisor中可以用这个名字来管理该程序。
[program:app]
#程序的启动目录,相当于在该目录下执行程序
directory = /usr/local/bin
# 程序的启动命令,与命令行启动的命令是一样的
command = /usr/local/bin/app -config-file /etc/confd/app.conf
# 可以通过 environment 来添加需要的环境变量,一种常见的用法是修改 PYTHONPATH
environment = PYTHONPATH=$PYTHONPATH:/path/to/somewhere
# 在 supervisord 启动的时候也自动启动
autostart = true
# 启动 5 秒后没有异常退出,就当作已经正常启动了
startsecs = 5
# 程序异常退出后自动重启
autorestart = true
# 启动失败自动重试次数,默认是 3
startretries = 3
# 用哪个用户启动
user = root
# 把 stderr 重定向到 stdout,默认 false,是否将程序错误信息重定向的到文件
redirect_stderr = true
# stdout 日志文件大小,默认 50MB
stdout_logfile_maxbytes = 20MB
# stdout 日志文件备份数
stdout_logfile_backups = 20
stdout_logfile = /var/log/app.out.log
# 程序错误信息输出到该文件
stderr_logfile = /var/log/app.err.log
web管理任务
参考文献:https://www.jianshu.com/p/b2a2bd0c81c5
https://blog.csdn.net/jackghq/article/details/62937208
https://www.centos.bz/2018/06/centos7-3%E9%85%8D%E7%BD%AEsupervisor%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%B0%8F%E9%97%AE%E9%A2%98/
# 修改配置文件
/etc/supervisor/supervisord.conf
# 添加下列配置
[inet_http_server]
# 对所有ip都可以访问
port=0.0.0.0:9001
# 指定web管理端登录用户名
username=admin
# 指定web端管理密码
password=admin
重载supervisor
# 最后重载supervisor
supervisorctl reload
Centos6安装Supervisor
依赖Centos6
的epel源
: https://blog.csdn.net/omaidb/article/details/146447932
# 安装supervisor
yum install -y supervisor
# 设置supervisor开机自启动
chkconfig supervisord on
# 查看supervisor是否开机自启动成功
chkconfig supervisord --list
编辑配置
Centos6版本的supervisor不支持子配置.
# 编辑主配置文件
vim /etc/supervisord.conf
# 直接在最下方添加配置
Centos7配置Supervisor
1. 安装 Supervisor
Supervisor
的文档地址:http://www.supervisord.org/
# 启用epel库
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# 安装supervisor
yum install -y supervisor
# 开机自启
sudo systemctl enable --now supervisord
有时候Centos7的epel源搜索不到该软件包,从https://centos.pkgs.org/7/epel-x86_64/supervisor-3.4.0-1.el7.noarch.rpm.html下载rpm安装包.
# Centos7下载rpm安装包
wget -c ftp://ftp.icm.edu.pl/vol/rzm3/linux-oracle-repo/OracleLinux/OL7/developer_EPEL/x86_64/supervisor-3.4.0-1.el7.noarch.rpm
# 已经失效的地址
wget -c https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/s/supervisor-3.4.0-1.el7.noarch.rpm
2. 配置 Supervisor
Supervisor 的配置文件为:/etc/supervisord.conf
Supervisor 所管理的应用的配置文件放在/etc/supervisord.d/
目录中,这个目录可以在 supervisord.conf
中配置。
常见问题
如果在启动的时候遇到如下错误:
Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program
使用以下命令:
# 查找supervisor.sock文件
find / -name supervisor.sock
# 找到supervisor.sock,然后unlink即可
unlink /path/supervisor.sock
4. app配置文件
Supervisor 管理应用的进程,需要对每个应用进行配置。在/etc/supervisord.d
中创建app.ini
,每个应用对应一个配置文件即可。
下面是配置文件的示例:
配置文件中的可执行程序
尽量使用绝对路径
vim /etc/supervisord.d/app.ini
[supervusird]
# 禁止后台运行
nodaemon=true
# 程序的名称
[program:helloworld]
# 执行的命令
command=/usr/bin/dotnet HelloWorld.dll
# 程序工作目录
directory=/root/www/
# 环境变量
environment=ASPNETCORE__ENVIRONMENT=Production
# 执行进程的用户
user=root
# 停止方式
stopsignal=INT
# 结束子进程(下面两项配置)
stopasgroup=true
# 强制终止时,整个进程组退出
killasgroup=true
# 是否自动启动
autostart=true
# 是否自动重启
autorestart=true
# 自动重启间隔
startsecs=1
# 错误输出路径
stderr_logfile=/var/log/helloworld.err.log
# 日志输出路径
stdout_logfile=/var/log/helloworld.out.log
配置示例
创建好配置文件后,重启 Supervisor
# 重启Supervisor
sudo systemctl restart supervisord
# 或热重启,不会重启其他子进程
supervisorctl reread
# 使用update命令,也会启动脚本
supervisorctl update
supervisorctl执行错误
参考:https://learnku.com/articles/48865
出现 error: <class ‘socket.error’>, [Errno 2] No such file or directory: file: /usr/lib64/python2.7/socket.py line: 224
错误
# 解决方法使用下面命令启动
/usr/bin/python2 /usr/bin/supervisord -c /etc/supervisord.conf
同时要保证*.ini
配置文件中的空格
,程序路径
,目录
是否正确/存在
;
为确保没有错误,可以正常启动,使用前文提到的查看Supervisor状态的命令
查看。或者查看要管理的进程是否启动,本例中可以使用下面的命令:
# 查看app的运行状态
supervisorctl status
# 查看程序进程状态
ps -ef | grep HelloWorld.dll
# 或
ps -ef | grep dotnet
5. Supervisor 管理进程
有两种方式可以管理进程,命令行和Web管理。
# 关闭所有任务
supervisorctl shutdown
# 暂停任务/开始任务
supervisorctl stop|start program_name
# 查看所有任务状态
supervisorctl status
Web管理
启用 Web 管理,首先将/etc/supervisord.conf
配置文件中的inet_http_server节点
取消注释。
# 修改后更新app
supervisorctl update
# 重载app
supervisorctl reload
下面是第三方Supervisor
的Web管理工具
:
cesi
: https://github.com/Gamegos/cesi
suponoff
: https://github.com/GambitResearch/suponoff
gosuv
: https://github.com/codeskyblue/gosuv
supervisord-monitor
: https://github.com/mlazarov/supervisord-monitor
supervisord-monitor改进版
: https://github.com/WisZhou/supervisord-monitor
supervisor实时查看日志
# 实时查看日志
supervisorctl tail -f <服务名>