Supervisor( http://supervisord.org/ )It is a client/server service developed in Python. It is a process management tool under Linux/Unix system and does not support Windows system. It can easily monitor, start, stop and restart one or more processes. For a process managed by supervisor, when a process is accidentally killed, supervisor will automatically pull it up after listening to the death of the process. It is very convenient to achieve the function of automatic recovery of the process, and there is no need to write your own shell script to control it.
1. Install Python package management tools( easy_install)
easy_install is a command in the setuptools package. Use easy_install is actually calling setuptools to complete the work of installing modules, so you can install setuptools.
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
2. Install supervisor
After supervisor is installed, three execution programs will be generated: supervisortd, supervisorctl and echo_supervisord_conf, which are respectively the daemon service of Supervisor (used to receive process management commands), client (used to communicate with the daemon and send instructions of the management process), and the program for generating the initial configuration file.
You can run echo_ supervisord_ The conf program generates the initialization configuration file of supervisor, as shown below:
mkdir /etc/supervisor echo_supervisord_conf > /etc/supervisor/supervisord.conf
4. Description of configuration file parameters
There are many configuration parameters for supervisor. The following describes the common parameter configuration. For detailed configuration and description, please refer to Official documents Introduction.
Note: semicolon (;) The configuration at the beginning indicates a comment
[unix_http_server] file=/tmp/supervisor.sock ;UNIX socket Documents, supervisorctl Can use ;chmod=0700 ;socket Document mode，The default is 0700 ;chown=nobody:nogroup ;socket Document owner，Format: uid:gid ;[inet_http_server] ;HTTP Server, providing web Management interface ;port=127.0.0.1:9001 ;Web Manage background running IP And ports. If you open to the public network, you need to pay attention to security ;username=user ;User name of login management background ;password=123 ;Password for login management background [supervisord] logfile=/tmp/supervisord.log ;Log file, default is $CWD/supervisord.log logfile_maxbytes=50MB ;Log file size, exceeding rotate，Default 50 MB，If set to 0, it means that the size is not limited logfile_backups=10 ;The number of reserved backups of log files is 10 by default. If it is set to 0, it means no backup loglevel=info ;Log level, default info，other: debug,warn,trace pidfile=/tmp/supervisord.pid ;pid file nodaemon=false ;Whether to start in the foreground. The default is false，Namely daemon Start by minfds=1024 ;The minimum value of the file descriptor that can be opened. The default value is 1024 minprocs=200 ;The minimum number of processes that can be opened. The default is 200 [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; Connect supervisor through UNIX socket, and the path is the same as UNIX_ http_ The file of server part is consistent ;serverurl=http://127.0.0.1:9001 ; Connect to supervisor via HTTP ; [program:xx]Is the configuration parameter of the managed process, xx Is the name of the process [program:xx] command=/opt/apache-tomcat-8.0.35/bin/catalina.sh run ; Program start command autostart=true ; stay supervisord It also starts automatically when starting startsecs=10 ; If there is no abnormal exit after 10 seconds of startup, it means that the process is started normally. The default is 1 second autorestart=true ; Automatic restart after program exit,Optional values:[unexpected,true,false]，Default to unexpected，Indicates 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 is used to start the process? The default is root priority=999 ; The process startup priority is 999 by default, and the one with small value will be started first redirect_stderr=true ; hold stderr Redirect to stdout，default false stdout_logfile_maxbytes=20MB ; stdout Log file size, default 50 MB stdout_logfile_backups = 20 ; stdout The number of log file backups. The default is 10 ; stdout The log file cannot be started normally when the specified directory does not exist, so you need to create the directory manually( supervisord (log files are created automatically) stdout_logfile=/opt/apache-tomcat-8.0.35/logs/catalina.out stopasgroup=false ;Default to false,Whether to send to this process group when the process is killed stop Signals, including child processes killasgroup=false ;Default to false，Send to process group kill Signals, including child processes ;Include other configuration files [include] files = relative/directory/*.ini ;You can specify one or more to.ini End profile
5. Configuration management process
Process management configuration parameters are not recommended to be written in supervisor In the conf file, you should write a configuration file for each process, put it in the directory specified by include, and include it in supervisor Conf file.
1. Create / etc / Supervisor / config D directory, which is used to store the configuration files of process management
2. Modify / etc / Supervisor / Supervisor Add the / etc/supervisor/conf.d directory to the include parameter in conf
[include] files = /etc/supervisor/config.d/*.ini
Examples are as follows:
[root@web03-uat config.d]# pwd /etc/supervisor/config.d [root@web03-uat config.d]# cat accept_event.ini [program:accept_event] command=/alidata/server/php72/bin/php /alidata/www/t-api-loreal-scrm.tarsocial.com/ma/artisan z:accept_event autostart=true startsecs=10 autorestart=true startretries=3 user=www priority=999 redirect_stderr=true stdout_logfile_maxbytes=20MB stdout_logfile_backups = 20 stdout_logfile=/etc/supervisor/logs/accept_event.out stopasgroup=false killasgroup=false
6. Control process
6.1 interactive terminal
After the supervisor is started successfully, the process can be controlled through the supervisor CTL client to start, stop and restart. Run the supervisorctl command without parameters. It will enter the interactive terminal of the supervisor client and list all the processes currently managed.
Enter help to view the list of executable commands. If you want to see the role of a command, run the help command name, such as help stop
stop accept_event // Indicates that the process is stopped stop all // Indicates that all processes are stopped // ...
6.2 bash terminal
supervisorctl status supervisorctl stop tomcat supervisorctl start tomcat supervisorctl restart tomcat supervisorctl reread supervisorctl update
6.3 Web management interface
For security reasons, the default configuration is that the web management interface is not enabled, and the supervisor.com needs to be modified The conf configuration file opens the http access permission and configures the following:
;[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))
Amend to read:
[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: bind the access IP and port. Here, bind the local IP and 9001 port
username: the user name of the login management background
Password: the password to log in to the management background
7. Start the Supervisor service
7.1 configuring systemctl service
1. Enter the / lib/systemd/system directory and create supervisor Service file
[Unit] Description=supervisor After=network.target [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
2. Set startup
systemctl enable supervisor.service systemctl daemon-reload
3. Modify file permissions to 766
chmod 766 supervisor.service
The Supervisor can only manage non daemon processes, that is, the Supervisor cannot manage daemons. Otherwise, you will be prompted with an exception of Exited too quickly (process log may have details).
Install using yum
yum install epel-release yum install -y supervisor
supervisor is not published in the standard CentOS source, and epel source needs to be installed. The latest version may not be installed in this way, but it is more convenient. After the installation is completed, the configuration file will be automatically generated for you.
Default configuration file: / etc / Supervisor conf
Put the process management configuration file in: / etc / Supervisor / config D directory
Default log file: / TMP / Supervisor Log to view the startup information of the process