supervisor

在了解supervisor + gunicorn +
flask部署、日志管理之后,接下来则需要supervisor稳定的服务。因为,当服务器重启,supervisor并不会重启,因此就需要一个Linux系统工具
Systemd 来管理它;

Systemd常用命令

# 设置开机启动
$ systemctl enable supervisor.service
# 启动服务
$ systemctl start supervisor.service
# 停止服务
$ systemctl stop supervisor.service
# 停止服务
$ systemctl restart supervisor.service
# 重新加载修改的配置文件
$ sudo systemctl daemon-reload
# 查看状态
$ systemctl status supervisor.service
$ sudo sdske可journalctl -u supervisor.service -f

```css
生成 supervisor 配置文件
在使用Systemd管理supervisor之前,首先先配置supervisor

$ echo_supervisord_conf > /etc/supervisor/supervisor.conf 
/etc/supervisor/ 、/etc/supervisor/conf.d 文件夹不存在,自行创建;
修改默认配置,本文中是通过include方式加载,以防止后续默认配置被覆盖等误操作操作,include加载方式如下:

$ vim /etc/supervisor/supervisor.conf
配置supervisord日志

[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=1;文件保留数,减少产生冗余日志文件,根据实际需求调整
配置inet_http_server(监听)

;根据实际需求调整
;[inet_http_server]         ; inet (TCP) server disabled by default
;port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)
取消 include 注释,并修改为

[include]
;files = relative/directory/*.ini
files = ./conf.d/*.ini
配置[program:theprogramname]

$ cat /etc/supervisor/conf.d/test.ini
[program:test]
command = /usr/虚拟环境python全路径/bin/python /usr/local/bin/gunicorn -c g_conf.py run:app --log-level=debug --preload
directory=/data/项目路径
startsecs=0
stopwaitsecs=0
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile = /data/logs/项目名称/stdout.log
stderr_logfile = /data/logs/项目名称/error.log
stdout_logfile_backups=1;文件保留数
stderr_logfile_backups=1;文件保留数


supervisor、gunicorn可以启动任意版本python(环境无问题的条件下),因为它们对脚本起到的是管理作用,所以也可以启动其他语言脚本;
在 /etc/supervisor/conf.d/ 目录下,可创建多个 *.ini 文件,即多个不同项目,示例如下
$ ls -lh /etc/supervisor/conf.d/
total 16K
-rw-r–r-- 1 root root 471 Nov 19 14:50 test.ini
-rw-rw-r-- 1 root root 476 Nov 19 14:50 test1.ini
-rw-r–r-- 1 root root 486 Nov 19 14:51 test2.ini
-rw-r–r-- 1 root root 512 Nov 19 14:52 test3.ini
或者将 /etc/supervisor/conf.d/ 目录下所有的 *.ini 文件合并为一个文件,格式如下
[program:test]
[program:test1]
[program:test2]
[program:test3]

至此,supervisor配置完成

配置supervisor的service
sudo vim /usr/lib/systemd/system/supervisor.service
加入如下内容

supervisord service for systemd (CentOS 7.0+)

by ET-CS (https://github.com/ET-CS)

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

Systemd管理supervisor
激活开机自动启supervisord(下面两种方式,下同)

$ systemctl enable supervisor.service
$ systemctl enable supervisor
启动supervisor

$ systemctl start supervisor.service
$ service supervisor start
本人喜欢用service supervisor start方式启动;
重载supervisor配置

$ systemctl reload supervisor.service
$ service supervisor reload
关闭supervisor服务

$ systemctl stop supervisor.service
$ service supervisor stop
查看supervisor状态

$ systemctl status supervisor.service
$ service supervisor status
● supervisor.service - Supervisor daemon
Loaded: loaded (/usr/lib/systemd/system/supervisor.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-11-19 10:35:29 CST; 7h ago
Main PID: 219632 (supervisord)
CGroup: /system.slice/supervisor.service
├─ 6399 /…/bin/python /usr/local/bin/gunicorn -c test_g_conf.py test:app --log-level=debug --preload
├─ 6400 /…/bin/python /usr/local/bin/gunicorn -c g_conf.py run:app --log-level=debug --preload
├─ 6401 /…/bin/python /usr/local/bin/gunicorn -c g_conf.py run:app --log-level=debug --preload
├─ 6402 /…/bin/python /usr/local/bin/gunicorn -c g_conf.py run:app --log-level=debug --preload
├─ 6789 /…/bin/python /usr/local/bin/gunicorn -c g_conf.py run:app --log-level=debug --preload
└─219632 /usr/bin/python2 /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
Nov 19 10:35:29 … systemd[1]: Starting Supervisor daemon…
Nov 19 10:35:29 … systemd[1]: Started Supervisor daemon.
另外,也可以使用supervisord和supervisorctl命令对supervisor进行管理(Systemd是否配置,均不影响)

#启动supervisor服务
$ supervisord -c /etc/supervisor/supervisord.conf
#更新配置,在更改/etc/supervisor/conf.d文件配置后
#下列操作等同于supervisorctl -c /配置路径/supervisord.conf 命令
$ supervisorctl update
test: stopped
test: updated process group
test1: stopped
test1: updated process group
test2: added process group
test3: added process group
#重载配置,对[program:theprogramname]之外进行配置,建议使用reload,update可能更新失效
$ supervisorctl reload
#查看状态
$ supervisorctl status
test RUNNING pid 6401, uptime 0:00:39
test1 RUNNING pid 6400, uptime 0:00:39
test2 RUNNING pid 6402, uptime 0:00:39
test3 RUNNING pid 6399, uptime 0:00:39
#关闭supervisor
$ supervisorctl shutdown
更多命令请看: supervisor 常用命令的使用
或者查看官方文档: Running Supervisor
supervisorctl update #更新所有
supervisorctl update test #更新test服务
supervisorctl restart #重启所有
supervisorctl restart test #重启test服务
supervisorctl restart test:* #批量重启test开头服务
supervisorctl restart test test1 #重启test、test1服务


你可能感兴趣的:(linux,运维,服务器)