basic introduction
Supervisor is a client/server system that allows users to control many processes on UNIX-like operating systems. It is a process management tool developed based on the python language.
The server side of Supervisor is called supervisord, which is mainly responsible for starting the managed subprocess when it starts itself, responding to client commands, restarting the crashed or exiting subprocess, recording the output of the subprocess stdout and stderr, generating and processing the life cycle of the subprocess event. You can configure related parameters in a configuration file, including the state of Supervisord itself, and the related attributes of each subprocess it manages. The configuration file is generally located in /etc/supervisord.conf.
The Supervisor client is called supervisorctl, which provides a shell-like interface (ie, command line) to use the functions provided by the supervisord server. Through supervisorctl, users can connect to the supervisord server process, get the status of the child process controlled by the server process, start and stop the child process, and get the list of running processes. The client communicates with the server through a Unix domain socket or a TCP socket, and the server has an identity credential authentication mechanism, which can effectively improve security. When the client and the server are on the same machine, the client and the server share the same configuration file /etc/supervisord.conf, and the configurations of the two are distinguished by different labels.
Supervisor also provides a web page to view and manage the status of the process, this function is less used.
Official website: http://supervisord.org
Installation environment preparation
System environment
[root@nginx ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@nginx ~]# uname -r
3.10.0-693.17.1.el7.x86_64
[root@nginx ~]# python --version
Python 2.7.5
If the python environment is not installed, you can use yum install python -y to install
Platform requirements
Supervisor can run on most Unix systems, but it does not support running on Windows systems.
Supervisor requires Python 2.4 and above, but it is not supported by any Python 3 version.
Install supervisor serviceInstall easy_install
[root@nginx ~]# yum install python-setuptools-devel -y
Install supervisor
easy_install supervisor
After the supervisor is installed, three execution programs will be generated:
supervisortd
#supervisor's daemon service (used to receive process management commands)
supervisorctl
#Client (used to communicate with the daemon and send instructions for the management process)
echo_supervisord_conf
#Generate initial configuration file program
Create a configuration file
Generally, the initialization configuration file of the supervisor can be generated by running the echo_supervisord_conf program
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
[root@ ~]# ll /etc/supervisor/supervisord.conf
-rw-r--r-- 1 root root 9710 Jan 24 15:10 /etc/supervisor/supervisord.conf
Configuration file parameter introduction[unix_http_server]
file=/tmp/supervisor.sock ;UNIX socket file, supervisorctl will use
;chmod=0700 ;The mode of the socket file, the default is 0700
;chown=nobody:nogroup ;The owner of the socket file, format: uid:gid
;[inet_http_server]; HTTP server, providing web management interface
;port=127.0.0.1:9001
; The IP and port running in the web management background, if open to the public network, you need to pay attention to security
;username=user ;The user name to log in to the management background
;password=123 ;The password to log in to the management background
[supervisord]
logfile=/tmp/supervisord.log
;Log file, the default is $CWD/supervisord.log
logfile_maxbytes=50MB
; The log file size will be rotated if it exceeds, the default is 50MB, if it is set to 0, it means that the size is not limited
logfile_backups=10; The default number of log file backups is 10, set to 0 means no backup
loglevel=info; log level, default info, others: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=false; Whether to start in the foreground, the default is false, that is, start in daemon mode
minfds=1024; The minimum value of file descriptors that can be opened, the default is 1024
minprocs=200; The minimum number of processes that can be opened, the default is 200
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock
; Connect supervisord through UNIX socket, the path is consistent with the file in the unix_http_server part
;serverurl=http://127.0.0.1:9001
; Connect supervisord via HTTP
[program:xx]
; [program:xx] is the configuration parameter of the managed process, xx is the name of the process
command=/opt/tomcat/bin/catalina.sh run
; Program start command
autostart=true; Automatically start when supervisord starts
startsecs=10; There is no abnormal exit after 10 seconds of startup, it means the process started normally, the default is 1 second
autorestart=true; Automatically restart after the program exits, optional value: [unexpected,true,false], the default is unexpected, which means that the process is restarted after being killed unexpectedly
startretries=3; The number of automatic retries after startup failure, the default is 3
user=tomcat; Which user to start the process, the default is root
priority=999; The process start priority, the default is 999, the value is small and the priority is started
redirect_stderr=true; redirect stderr to stdout, default false
stdout_logfile_maxbytes=20MB; stdout log file size, default 50MB
stdout_logfile_backups = 20; The number of stdout log file backups, the default is 10
; stdout log file, you need to pay attention that it cannot start normally when the specified directory does not exist, so you need to create the directory manually (supervisord will automatically create the log file)
stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out
stopasgroup = false
; The default is false, when the process is killed, whether to send a stop signal to this process group, including child processes
killasgroup=false
; The default is false, send a kill signal to the process group, including child processes
[include] ;Include other configuration files
files = relative/directory/*.ini
; You can specify one or more configuration files ending in .ini
Note: in the configuration file; is a comment
Introduction to commonly used commandsAfter supervisord starts successfully, you can control the process through the supervisorctl client to start, stop, and restart. Run the supervisorctl command without adding parameters, you will enter the interactive terminal of the supervisor client, and will list all the processes currently managed.
[root@java-test ~]# supervisorctl
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
You can view the usage and parameters of related commands through the help command
Configuration management processThe following is an example of configuring the Tomcat process:
[program:tomcat]
command=/usr/local/tomcat/bin/catalina.sh run
stdout_logfile=/usr/local/tomcat/logs/catalina.out
autostart=true
autorestart=true
startsecs=5
priority=1
stopasgroup=true
killasgroup=true
After the configuration is complete, start the service
supervisord -c /etc/supervisor/supervisord.conf
After startup, you need to perform the following operations to update the configuration file
[root@ ~]# supervisorctl update
[root@ ~]# supervisorctl status
tomcat RUNNING pid 12223, uptime 0:06:31
Terminal commands
supervisorctl status
supervisorctl stop tomcat
supervisorctl start tomcat
supervisorctl restart tomcat
So far, the related installation and configuration introduction is over. Generally, in actual production, it is not recommended to write all the configurations in the same configuration file, but can be written separately.