Linux process management tool Supervisor detailed explanation and actual combat

1. Introduction

Supervisor (http://supervisord.org) is a client/server service developed in Python. It is a process management tool under Linux/Unix systems and does not support Windows systems. It can easily monitor, start, stop, and restart one or more processes. When a process is managed by Supervisor, when a process is accidentally killed, supervisort will automatically re-pull it after it detects that the process has died. It is very convenient to achieve the function of automatic process recovery, no longer need to write a shell script to control it.
Since Supervisor is developed by Python, check whether the system has Python 2.4 or higher before installation.

Second, the installation of Supervisor

1. Environmental information

系统版本:CentOS 7.7.1908 
Python版本:Python 3.7.3

If the python version is lower than 2.6, please upgrade, see python3 installation for details

2. Install Supervisor (three methods)

1) easy_install installation

easy_install is a command in the setuptools package. Using easy_install is actually calling setuptools to complete the work of installing the module, so just install setuptools.

wget https://pypi.io/packages/source/s/setuptools/setuptools-33.1.1.zip
unzip setuptools-33.1.1.zip
cd setuptools-33.1.1
python setup.py install
easy_install supervisor

2) pip installation

Use pip to install, the premise must ensure that the pip version is greater than 2.6

pip install supervisor
或
pip3 install supervisor

3) Yum installation

yum install -y epel-release
yum install -y supervisor

3. Introduction to superviso program, configuration steps and common commands

1. Program introduction

After the supervisor is installed, three execution programs will be generated: supervisortd, supervisorctl, echo_supervisord_conf:

supervisortd:用于管理supervisor本身服务
supervisorctl:用于管理我们需要委托给superviso工具的服务
echo_supervisord_conf:用于生成superviso的配置文件
supervisor的守护进程服务(用于接收进程管理命令)
客户端(用于和守护进程通信,发送管理进程的指令)
[root@node02 ~]# which supervisord
/usr/local/python3/bin/supervisord
[root@node02 ~]# which supervisorctl
/usr/local/python3/bin/supervisorctl
[root@node02 ~]# which echo_supervisord_conf
/usr/local/python3/bin/echo_supervisord_conf

Insert picture description here

2. Configure Supervisor

1) Initialize the configuration file

Generate the initial configuration file of the supervisor by running the echo_supervisord_conf program.
If you use yum to install, omit this step and proceed directly to the step of modifying the configuration file

mkdir /etc/supervisord.d
echo_supervisord_conf > /etc/supervisord.conf

2) Modify the configuration file

There are a lot of supervisor configuration files, but many of them can be used without modification. Only the following two items have been modified here

sed -i 's/;chmod=0700/chmod=0766/g' /etc/supervisord.conf  #修改socket文件的mode,默认是0700 
sed -i '$a [include] \
files = /etc/supervisord.d/*.conf' /etc/supervisord.conf  #在配置文件最后添加以下两行内容来包含/etc/supervisord目录

3. Introduction to common commands

supervisorctl 是 supervisord的命令行客户端工具
supervisorctl status:查看所有进程的状态
supervisorctl stop es:停止es
supervisorctl start es:启动es
supervisorctl restart es: 重启es
supervisorctl update :配置文件修改后可以使用该命令加载新的配置
supervisorctl reload: 重新启动配置中的所有程序

Four, write the process that needs to be managed by Supervisor

Supervisor can only manage non-dameon processes. For example, the default redis runs in the foreground. Tomcat actually uses startup.sh shutdown.sh to call catalina.sh to run in the background. The default catalina.sh is a program running in the foreground and cannot be managed like Nginx. Non-dameon process.

1. Supervisor manages tomcat service

1) Install tomcat ignore

2) Configure the tomcat file of Supervisor

cat <<EOF>>  > /etc/supervisord.d/tomcat.conf
[program:tomcat]                                        #程序唯一名称
directory=/root/apache-tomcat-9.0.35                    #程序路径
command=/root/apache-tomcat-9.0.35/bin/catalina.sh run  #运行程序的命令
autostart=true                                          #是否在supervisord启动后tomcat也启动
startsecs=10                                            #启动10秒后没有异常退出,就表示进程正常启动了,默认为1秒
autorestart=true                                        #程序退出后自动重启,可选值:[unexpected,true,false],默认为unexpected,表示进程意外杀死后才重启;意思为如果不是supervisord来关闭的该进程则认为不正当关闭,supervisord会再次把该进程给启动起来,只能使用该supervisorctl来进行关闭、启动、重启操作 
startretries=3                                          #启动失败自动重试次数,默认是3
user=root                                               #用哪个用户启动进程,默认是root
priority=999                                            #进程启动优先级,默认999,假Supervisord需要管理多个进程,那么值小的优先启动
stopsignal=INT
redirect_stderr=true                                    #把stderr重定向到stdout标准输出,默认false
stdout_logfile_maxbytes=200MB                           #stdout标准输出日志文件大小,日志文件大小到200M后则进行切割,切割后的日志文件会标示为catalina.out1,catalina.out2,catalina.out3...,默认50MB
stdout_logfile_backups = 100                            #stdout标准输出日志文件备份数,保存100个200MB的日志文件,超过100个后老的将被删除,默认为10保存10个
stdout_logfile=/root/apache-tomcat-9.0.35/logs/catalina.out      #标准日志输出位置,如果输出位置不存在则会启动失败
stopasgroup=false                                       #默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
killasgroup=false                                       #默认为false,向进程组发送kill信号,包括子进程
EOF

Insert picture description here

3) Program management

supervisorctl status tomcat                             #tomcat状态
supervisorctl stop tomcat                               #停止tomcat
supervisorctl start tomcat                              #启动tomcat
supervisorctl restart tomcat                            #重启tomcat
supervisorctl reoload tomcat                            #重仔tomcat

Insert picture description here

2. Supervisor management es service

1) Install es ignore

2) Configure the es file of Supervisor

cat  <<EOF>>  >  /etc/supervisord.d/es.conf 
[program:es]
directory=/u01/isi/application/index/elasticsearch-6.5.1
command=/u01/isi/application/index/elasticsearch-6.5.1/bin/elasticsearch
user=isi
priority=998
stdout_logfile=/u01/isi/application/index/elasticsearch-6.5.1/logs/wenhai-cluster.log
autostart=true
autorestart=true
startsecs=60
stopasgroup=true
killasgroup=true
startretries=3
redirect_stderr=true
stdout_logfile_maxbytes=200MB
stdout_logfile_backups = 100
EOF

Insert picture description here

3) Program management

supervisorctl reload  #重载一下即可
supervisorctl status

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44729138/article/details/106529821