supervisor 管理应用程序(2021.5.11更新)

supervisor 进程管理

主要包含后台进程 supervisord 和控制台 supervisorctl 两个程序

supervisor
# 官方文档 http://www.supervisord.org/installing.html

# 此程序是用python开发,python2.7.x 运行中没有发现问题.
# 目标机器没有pip, 所以使用离线安装. 下载以下三个压缩包最新版: # https:
//pypi.org/pypi/supervisor/ # https://pypi.org/pypi/setuptools/ # https://pypi.org/pypi/meld3/ # 复制到目标机器, 解压,安装. sudo tar zxf meld3-2.0.0.tar.gz sudo tar zxf supervisor-4.0.4.tar.gz sudo unzip setuptools-41.2.0.zip cd setuptools-41.2.0 sudo python setup.py install cd ../meld3-2.0.0 sudo python setup.py install cd ../supervisor-4.0.4 sudo python setup.py install # 留意安装信息显示的安装位置 ... Installing echo_supervisord_conf script to /usr/bin Installing supervisorctl script to /usr/bin Installing supervisord script to /usr/bin # 生成配置文件示例格式,可在此基础修改 echo_supervisord_conf > /etc/supervisor/supervisord.conf # ------------------------- supervisord.conf --------------------------- [unix_http_server] file=/tmp/supervisor.sock ; (the path to the socket file) [inet_http_server] port=0.0.0.0:9001 username=admin password=123456 [supervisord] logfile=/tmp/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=/tmp/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:///tmp/supervisor.sock ; use a unix:// URL for a unix socket serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket username=admin ; should be same as http_username if set password=123456 ; should be same as http_password if set [include] files = conf.d/*.conf

# 使用包含子目录的方式,指定单个接管的程序 # ------------------------- /etc/supervisor/conf.d/test.demo.conf --------------- [program:test.demo] command=dotnet Csharp_class.dll directory=/webapp/test.demo/ process_name=%(program_name)s stderr_logfile=/var/log/test.demo.log stdout_logfile=/var/log/test.demo.log environment=ASPNETCORE_ENVIRONMENT=Production user=root stopsignal=INT autostart=true autorestart=true startsecs=3 # 启动服务 # 可以 -c 指定配置文件,也可不指定,程序将自动寻找 supervisor.conf sudo supervisord -c /etc/supervisor/supervisord.conf # 注意:一台机器不能有多个同名配置文件. # 某服务器就因为有 /etc/supervisord.conf 但是启动服务用的 -c /etc/supervisor/supervisord.conf # 两处配置文件内容不同导致进入 supervisorctl 始终出现错误: unix ///var/run/supervisor.sock refused connection # ----------------------------- shell ---------------------------------- supervisorctl # 进入shell 如果配置正确,登录后将直接列出管理的程序 status # 列出状态 version # 版本 help shutdown # 结束 supervisord 服务 reread # 重读配置文件,列出变化 update # 重读配置文件并生效(默认更新全部),指定名称则更新单个
start test # 启动单个
start all # 启动全部
stop test # 停止单个
stop all # 停止全部
restart test # 重启单个
restart all # 重启全部 remove test.demo # 移除 test.demo
## 如果更改了配置文件,一定要先reread, 然后 update , # 直接 restart 只会重新载入程序代码,不会重载配置文件。 # 例如我改了/etc/supervisor/conf.d/demo.conf # 必须先reread, 提示发现变化后,再执行 update demo 才能让配置生效。

 

如果需要开机自动启动,可将其安装为系统服务:

# --------------- 安装为系统服务 ---------------------------------------
# 安装时会产生服务文件: cat /usr/lib/systemd/system/supervisord.service
# ll /etc/systemd/system/multi-user.target.wants 时可以
# 看到supervisord.service文件的真实链接
# 当 systemctl enable supervisord 时,则将此链接作为服务配置文件

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

[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


# 如果已经有运行的 supervisor及程序,需要先进入shell, stop all
# 然后再 kill -9 对应的supervisor主程序,使用服务管理的方式启动。
sudo systemctl enable supervisord
sudo systemctl start supervisord
sudo systemctl status supervisord
sudo systemctl list-unit-files   # 检查列表中是否存在并且 enabled

 systemd 参考: http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-commands.html

 

今天加入了一个nodejs程序, 添加配置文件到 /etc/supervisor/conf.d

[program:parsoid]
command=node bin/server.js
directory=/opt/parsoid
process_name=%(program_name)s
stderr_logfile=/opt/parsoid/logs/err.log
stdout_logfile=/opt/parsoid/logs/parsoid.log

user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3

依次执行下方测试:

supervisorctl

>reread  # 发现
>update  

>status
>stop parsoid   # 测试停止
>start parsoid   # 测试启动
>status

 

我现在的公司,主要用它来管理 dotnet 程序

centos7 安装 dotnet-sdk

rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
yum search dotnet
yum install dotnet-sdk-3.1
yum install dotnet-sdk-5.0

 

2021.5.11 更新

直接使用 yum 安装

yum install supervisor -y

# 编辑 /etc/supervisord.conf 注意以下这些行。其它有分号的行不显示。
[unix_http_server]
file=/var/run/supervisor/supervisor.sock   ; (the path to the socket file)

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; (ip_address:port specifier, *:port for all iface)
[supervisorctl]
serverurl=unix:///var/run/supervisor/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket

[include]
files = supervisord.d/*.conf

 

#启动服务
systemctl start supervisord
systemctl enable supervisord
#在 /etc/supervisord.d/中添加一个 app 配置文件 qt.dcms.webapi.conf
[program:qt.dcms.webapi.svc]
command=dotnet qt.dcms.webapi.dll
directory=/webapp/qt.dcms.webapi.publish/
process_name=%(program_name)s
stderr_logfile=/var/log/qt.dcms.webapi.error.log
stdout_logfile=/var/log/qt.dcms.webapi.stdout.log
environment=ASPNETCORE_ENVIRONMENT=Production
user=root
stopsignal=INT
autostart=true
autorestart=true
startsecs=3
# 进入 supervisor
supervisorctl
>
> reread
> update
> status

# 如果看到添加的 APP 已经 RUNNING,则运行成功

 

posted @ 2019-10-21 10:04  枫若雪  阅读(610)  评论(0编辑  收藏  举报