supervisor工具

2021-06-04 fishedee 后端

0 概述

supervisor,是一个常见的进程监控工具,在进程挂掉以后会自动重启进程

1 安装与启动

1.1 安装

1.1.1 Mac

sudo pip3 install supervisor

supervisor是一个Python的程序,所以用pip来安装

1.1.2 Linux

sudo yum -y install supervisor

通过yum源安装的supervisor,已经有默认的配置文件,以及systemctl的服务配置,比pip安装方便多了

sudo systemctl enable --now supervisor.service

启动supervisor服务

1.2 初始配置

sudo echo_supervisord_conf > /etc/supervisord.conf
sudo vim /etc/supervisord.conf

初始化配置文件,echo_supervisord_conf是一个范例配置,可执行文件。对于Mac来说,位置可以设置在/usr/local/etc/supervisord.conf

[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]
files = /etc/supervisor/*.conf

编辑/etc/supervisord.conf文件,在底端加入以上的这一段。这一段的意思是加入/etc/supervisor文件夹下的所有外部配置文件,一般这个文件夹下的都是各个需要被监控进程的配置文件。注意,[include]前面的分号要删掉。

inet_http_server就是打开Web界面管理,这个界面一般不打开。

1.3 启动

supervisord -c /etc/supervisord.conf

启动supervisor

ps aux | grep supervisord

可以看到进程

1.4 自启动supervisor自身

supervisor可以管理其他的服务进程,但是supervisor自身也是一个进程,它需要被一个系统进程管理来开机自启,以及掉线后自动重启

1.4.1 Mac

进入/Library/LaunchDaemons目录

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
    </dict>
    <key>Label</key>
    <string>supervisord</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/supervisord</string>
        <string>-n</string>
        <string>-c</string>
        <string>/usr/local/etc/supervisord.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

添加这个plist

sudo launchctl load /Library/LaunchDaemons/supervisord.plist

然后启动就可以了

# 查看系统服务
sudo launchctl list

# 下线某个服务
sudo launchctl load /Library/LaunchDaemons/supervisord.plist

launchctl的其他命令

2 配置服务

2.1 配置

[program:nginx]                                         ; 是应用程序的唯一标识,不能重复
directory = /usr/local/Cellar/nginx/1.19.3/bin          ; 程序的启动目录
command = /usr/local/Cellar/nginx/1.19.3/bin/nginx -g 'daemon off;' ; 启动命令
autostart = true                                        ; 在 supervisord 启动的时候也自动启动
startsecs = 5                                           ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true                                      ; 程序异常退出后自动重启
startretries = 3                                        ; 启动失败自动重试次数,默认是 3
redirect_stderr = true                                  ; 把 stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /usr/local/var/log/supervisor/nginx.log   ; stdout 日志文件,注意:要确保目录已经建立并且可以访问(写权限)

启动的时候,nginx必须要以前台的方式启动,所以有-g ’daemon off;’的参数,注意分号不能少。配置文件写在/etc/supervisor/nginx.conf文件。

[program:mm_erp]
directory = /home/fish/Project/mm_erp/
command = /usr/bin/java -jar /home/fish/Project/mm_erp/mm_erp-1.0-SNAPSHOT.jar --spring.profiles.active=production
user = jan
autostart = true
startsecs = 5
autorestart = true
startretries = 3
redirect_stderr = true
stdout_logfile_maxbytes = 20MB
stdout_logfile_backups = 20
stdout_logfile = /home/fish/Project/mm_erp/stdout.log

一个普通的java程序的supervisor,注意,supervisor可以用user配置来设定用哪个用户来执行程序

2.2 操控

supervisorctl status

可以看到服务的执行状态

supervisorctl stop nginx

停止服务

supervisorctl start nginx

启动服务

3 总结

一个普通但有用的工具,参考资料:

相关文章