Through the process of learning and sharing, I will summarize and output the problems and techniques in my work, hoping that both novices and old birds can gain new knowledge through their articles and put them into practice.

Software and Hardware Environment

  • 64 – bit centos7.6.1810
Cat /etc/redhat-release # Check the system versionCopy the code
  • The supervisor 3.4.0

  • Python 2.7.5

Introduction of the supervisor

Supervisor is a process management tool written in Python that allows you to easily listen to, start, stop, and restart one or more processes. When a process is accidentally killed, when the supervisor detects that the process is dead, it is very convenient for the process to automatically recover, without the need for programmers or system administrators to write their own code to control it.

Supervisord installation

yum install -y epel-release
yum install -y supervisor
Copy the code

Start & Open self – start

systemctl start supervisord
systemctl enable supervisord
Copy the code

Other commands

systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord
Copy the code

The supervisor of the web side

The Supervisor provides Web-based control, allowing administrators to start and restart processes by clicking a button.

Access the configuration file and enable the web support

vim /etc/supervisord.conf
Copy the code

If the port is provided for external access, change the port to the local IP address

# Uncomment lines 10-13 with line number [inet_http_server]; Inet (TCP) server disabled by default port=192.168.26.121: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))Copy the code

After the configuration, restart the service

systemctl restart supervisord
Copy the code

Supervisord application configuration

The supervisord configuration file is displayed

cat /etc/supervisord.conf
Copy the code

See the last line of the configuration file

[include]
files = supervisord.d/*.ini
Copy the code

In other words, all our application configuration files are stored in this directory, named in **. Ini ** format, you can change the address, but do not change the suffix

So let’s create a monitored application

Create the test Python configuration

Create an application configuration named Python

vim /etc/supervisord.d/python.ini
Copy the code

The contents of the configuration file, where command is the command that we need to execute to start our application

[program:python] Python is the monitoring name we display on the web front end and terminal
command=python /tmp/supervisordtest/test.py  # The file address we want to monitor
autostart=true
autorestart=true
startsecs=1
startretries=3
redirect_stderr=true
stdout_logfile=/tmp/supervisordtest/access_python.log   # Log address, you can configure the directory
stderr_logfile=/tmp/supervisordtest/error_python.log    # Log address, you can configure the directory
Copy the code

Create test. Py

mkdir /tmp/supervisordtest
vim /tmp/supervisordtest/test.py
Copy the code

Program content: Open an infinite loop, constantly print content

while True:
   print(100)
Copy the code

Restart the supervisord to make the configuration file take effect

systemctl restart supervisord
Copy the code

Check whether the application is started properly

1. Command view

systemctl status supervisord
Copy the code

2. Visual Web viewing

You can restart, stop, clear logs, and view logs on the Web

Supervisor commands

The supervisorctl, supervisord, and echo_supervisord_conf commands are generated

  1. Supervisord supervisord, run the supervisor will start a process, it is responsible for starting the management process, and the management process as a child processes to start, but also in the management process of collapse when the automatic restart

  2. Supervisorctl is the command line management tool. It is used to run start stop restart commands to manage these sub-processes, for example

    sudo supervisorctl start demoweb
    Copy the code

    Demoweb is the name of the process, and detailed commands and instructions are shown in the table below

    The command instructions
    supervisorctl start program_name Start a process
    supervisorctl stop program_name Stopping a process
    supervisorctl restart program_name Restart a process
    supervisorctl status program_name View the status of a process
    supervisorctl stop all | \ stop all process
    supervisorctl reload Load the latest configuration file and restart all processes
    supervisorctl update Restart the processes whose configurations have changed based on the latest configurations. The processes whose configurations have not been updated are not affected
  3. echo_supervisord_conf

    This is used to generate the default configuration file (the default configuration file, which is very complete and annotated, and suitable for reference when using, is used like this

echo_supervisord_conf > test.conf
Copy the code

END

Welcome to the public account programmer toolset 👍👍 is committed to sharing excellent open source projects, learning resources, commonly used tools

Reply to the keyword “Pay attention to the package” and give you the most complete map of programmer skills.

Reply keyword “WX” add personal wechat, hook up the author, welcome to chat ^-^.