Press "Enter" to skip to content

Supervisor安装与配置

Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统,它可以很方便的监听、启动、停止、重启一个或多个进程.用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制.

主机环境;
[root@test ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.4.1708 (Core)
Release: 7.4.1708
Codename: Core
[root@test ~]# uname -a
Linux test 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

安装具体步骤:
配置epel源,具体配置步骤可参考https://www.tracymc.cn/?p=581
[root@test home]# cat /etc/yum.repos.d/epel-7.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0

[root@test home]# yum info supervisor
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name : supervisor
Arch : noarch
Version : 3.1.4
Release : 1.el7
Size : 446 k
Repo : epel/x86_64
Summary : A System for Allowing the Control of Process State on UNIX
URL : http://supervisord.org/
License : ZPLv2.1 and BSD and MIT
Description : The supervisor is a client/server system that allows its users to control a
: number of processes on UNIX-like operating systems.
[root@test home]# yum install -y supervisor
.
.
.(中间省略若干行)
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
nux-dextop-release-0-5.el7.nux.noarch has missing requires of epel-release
webtatic-release-7-3.noarch has missing requires of epel-release >= ('0', '7', None)
Installing : python-meld3-0.6.10-1.el7.x86_64 1/2
Installing : supervisor-3.1.4-1.el7.noarch 2/2
Verifying : python-meld3-0.6.10-1.el7.x86_64 1/2
Verifying : supervisor-3.1.4-1.el7.noarch 2/2

Installed:
supervisor.noarch 0:3.1.4-1.el7

Dependency Installed:
python-meld3.x86_64 0:0.6.10-1.el7

Complete!
以上字样说明安装成功.安装成功默认配置文件为/etc/supervisord.conf.

supervisor的配置参数较多,下面介绍一下常用的参数配置,详细的配置及说明,请参考官方文档介绍(http://supervisord.org/configuration.html).
注:分号(;)开头的配置表示注释

[unix_http_server]
file=/tmp/supervisor.sock ;UNIX socket文件,supervisorctl会使用
;chmod=0700 ;socket文件的mode,默认是0700
;chown=nobody:nogroup ;socket文件的owner,格式:uid:gid

;[inet_http_server] ;HTTP服务器,提供web管理界面
;port=127.0.0.1:9001 ;Web管理后台运行的IP和端口,如果开放到公网.需要注意安全性
;username=user ;登录管理后台的用户名
;password=123 ;登录管理后台的密码

[supervisord]
logfile=/tmp/supervisord.log ;日志文件,默认是$CWD/supervisord.log
logfile_maxbytes=50MB ;日志文件大小,超出会rotate,默认50MB,如果设成0,表示不限制大小
logfile_backups=10 ;日志文件保留备份数量默认10,设为0表示不备份
loglevel=info ;日志级别,默认info,其它:debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid文件
nodaemon=false ;是否在前台启动,默认是false,即以daemon的方式启动
minfds=1024 ;可以打开的文件描述符的最小值,默认1024
minprocs=200 ;可以打开的进程数的最小值,默认200

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ;通过UNIXsocket连接supervisord,路径与unix_http_server部分的file一致
;serverurl=http://127.0.0.1:9001 ;通过HTTP的方式连接supervisord

;[program:xx]是被管理的进程配置参数,xx是进程的名称
[program:xx]
command=/home/tomcat/bin/catalina.sh run ;程序启动命令
autostart=true ;在supervisord启动的时候也自动启动
startsecs=10 ;启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true ;程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启
startretries=3 ;启动失败自动重试次数,默认是3
user=tomcat ;用哪个用户启动进程,默认是root
priority=999 ;进程启动优先级,默认999,值小的优先启动
redirect_stderr=true ;把stderr重定向到stdout,默认false
stdout_logfile_maxbytes=20MB ;stdout日志文件大小,默认50MB
stdout_logfile_backups = 20 ;stdout日志文件备份数,默认是10
; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord会自动创建日志文件)
stdout_logfile=/home/tomcat/logs/catalina.out
stopasgroup=false ;默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false ;默认为false,向进程组发送kill信号,包括子进程

;包含其它配置文件
[include]
files = supervisord.d/*.ini ;可以指定一个或多个以.ini结束的配置文件

进程管理配置参数,不建议全都写在supervisord.conf文件中,应该每个进程写一个配置文件放在include指定的目录下包含进supervisord.conf文件中.
1.创建/etc/supervisord.d目录,用于存放进程管理的配置文件
2.修改/etc/supervisord.conf中的include参数,将/etc/supervisord.d目录添加到include中(默认配置文件已经写好了,无需自己添加)
下面是配置Tomcat进程的一个例子(同/etc/supervisord.conf中的program模块配置):
vi /etc/supervisord.d/tomcat.ini
[program:tomcat]
command=/home/tomcat/bin/catalina.sh run run
stdout_logfile=/home/tomcat/logs/catalina.out
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true

启动/重启/停止Supervisor服务:
[root@test supervisord.d]# /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf   //启动

[root@test yum.repos.d]# supervisorctl --help    //supervisorctl命令帮助
supervisorctl -- control applications run by supervisord from the cmd line.

Usage: /usr/bin/supervisorctl [options] [action [arguments]]

Options:
-c/--configuration -- configuration file path (default /etc/supervisord.conf)
-h/--help -- print usage message and exit
-i/--interactive -- start an interactive shell after executing commands
-s/--serverurl URL -- URL on which supervisord server is listening
(default "http://localhost:9001").
-u/--username -- username to use for authentication with server
-p/--password -- password to use for authentication with server
-r/--history-file -- keep a readline history (if readline is available)

action [arguments] -- see below

Actions are commands like "tail" or "stop". If -i is specified or no action is
specified on the command line, a "shell" interpreting actions typed
interactively is started. Use the action "help" to find out about available
actions.

[root@test supervisord.d]# supervisorctl -u test -p M2HSp6En85ex -c /etc/supervisord.conf status //查看配置文件状态
tomcat
[root@test supervisord.d]# supervisorctl -u test -p M2HSp6En85ex -c /etc/supervisord.conf reload //重新载入配置文件
Restarted supervisord
[root@test init.d]# supervisorctl stop all //先关闭supervisor启动脚本,之后再关闭supervisord服务
[root@test init.d]# ps -ef|grep supervisord
root 20060 1 0 17:09 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisord.conf
root 20590 19873 0 17:59 pts/1 00:00:00 grep --color=auto supervisord
[root@test init.d]# kill -9 20060
[root@test init.d]# ps -ef|grep supervisord
root 20592 19873 0 17:59 pts/1 00:00:00 grep --color=auto supervisord

启动、停止、重启tomcat(以停止为例):
方式一:交互终端
[root@test supervisord.d]# supervisorctl
tomcat RUNNING pid 20185, uptime 0:02:25
supervisor> stop tomcat
tomcat: stopped
supervisor> help //常见帮助命令

default commands (type help <topic>):
=====================================
add clear fg open quit remove restart start stop update
avail exit maintail pid reload reread shutdown status tail version

supervisor> help stop
stop <name> Stop a process
stop <gname>:* Stop all processes in a group
stop <name> <name> Stop multiple processes or groups
stop all Stop all processes

方式二:bash终端
[root@test supervisord.d]# supervisorctl status //查看进程状态
tomcat
supervisorctl stop tomcat
supervisorctl stop all //停止所有的进程
supervisorctl start tomcat
supervisorctl restart tomcat

方式三:web端重启服务
出于安全考虑,默认配置是没有开启web管理界面,需要修改supervisord.conf配置文件打开http访权限,将下面的配置:
;[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))
修改成:
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0: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))
port:绑定访问IP和端口,这里是绑定的是本地IP和9001端口
username:登录管理后台的用户名
password:登录管理后台的密码
配置修改完成后重新reload下:
[root@test supervisord.d]# supervisorctl -u test -p M2HSp6En85ex -c /etc/supervisord.conf reload
打开http://ip:9001输入用户名密码即可登录.web页面如下:

在web页面也能进行启动/停止/重启等操作.

开机启动Supervisor服务
配置systemctl服务(yum安装默认已经有了):
进入/lib/systemd/system目录,并创建supervisord.service文件
vi /lib/systemd/system/supervisord.service(yum安装默认已经有了)
[Unit]
Description=Process Monitoring and Control Daemon
After=rc-local.service nss-user-lookup.target

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf

[Install]
WantedBy=multi-user.target

设置开机启动:
chmod 644 supervisord.service
systemctl enable supervisord.service

注意:
Supervisor只能管理非daemon的进程,也就是说Supervisor不能管理守护进程。否则提示Exited too quickly (process log may have details)异常。例子中的Tomcat默认是以守护进程启动的,所以我们改成了catalina.sh run,以前台进程的方式运行.

2 Comments

  1. jonh jonh 三月 18, 2019

    您好,请问您是否遇到过如下问题:
    Internal Server Error
    The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

    安装完cesi后,用http://ip:5000进行访问,输入账号密码后提示的错误

    • tracymccn tracymccn 三月 19, 2019

      看看这篇文章有没有帮助https://www.tracymc.cn/archives/599

发表评论

电子邮件地址不会被公开。 必填项已用*标注