3 minutes to learn how to use the supervisor watchdog

Software and hardware environment

  • centos7.6.1810 64bit

    cat /etc/redhat-release #View system version
  • supervisor 3.4.0

  • python 2.7.5

Introduction to supervisor

Supervisor is a process management tool written in python language. It can easily monitor, start, stop and restart one or more processes. When a process is accidentally killed and the supervisor listens to the death of the process, it can easily make the process recover automatically, and there is no need for the programmer or system administrator to write code to control it.

Supervisor installation

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

Start & start self start

systemctl start supervisord
systemctl enable supervisord

Other commands

systemctl stop supervisord
systemctl start supervisord
systemctl status supervisord
systemctl reload supervisord
systemctl restart supervisord

web side of supervisor

supervisor provides web-based control. The administrator can start and restart the process by clicking a button on the page, which is very convenient.

Enter the configuration file to enable support for the web side

vim /etc/supervisord.conf

If you provide external access, you need to change the port to the local ip address

#Cancel the comment on lines 10-13, and the preceding number is the line number
[inet_http_server]         ; inet (TCP) server disabled by default
port=   ; (ip_address:port specifier, *:port for all iface)
username=user              ; (default is no username (open server))
password=123               ; (default is no password (open server))

Restart the service after configuration

systemctl restart supervisord

Supervisor application configuration

Enter the supervisor configuration file

cat /etc/supervisord.conf

See through the last line of the configuration file

files = supervisord.d/*.ini

In other words, all our application configuration files are saved in this directory for * * ini * * format name saved, you can modify the address, but do not modify the suffix

Let's create a monitored application

Create test python configuration

Create an application configuration called python

vim /etc/supervisord.d/python.ini

The content of the configuration file, where command is the command that needs to be executed when our application starts

[program:python] #python here is the monitoring name displayed on the web front end and terminal
command=python /tmp/supervisordtest/test.py  #The address of the file we want to monitor
stdout_logfile=/tmp/supervisordtest/access_python.log   #Log address, self configurable directory
stderr_logfile=/tmp/supervisordtest/error_python.log    #Log address, self configurable directory

Create test py

mkdir /tmp/supervisordtest
vim /tmp/supervisordtest/test.py

Program content: start an endless cycle and print the content continuously

while True:

Restart supervisor to make the configuration file effective

systemctl restart supervisord

Check whether the application starts normally

1. Command view

systemctl status supervisord

2. Visual web view

The web side can restart, stop, clean up logs, view logs and other operations

Several commands related to supervisor

After installation, three system commands, supervisorctl, supervisord and echo, will be generated_ supervisord_ conf

  1. Supervisor: when running supervisor, it will start a process supervisor. It is responsible for starting the managed process, starting the managed process as its own child process, and can automatically restart when the managed process crashes

  2. supervisorctl is a command line management tool that can be used to execute commands such as start stop restart to manage these sub processes, such as

    sudo supervisorctl start demoweb

    demoweb is the name of the process. See the following table for detailed commands and instructions

    command explain
    supervisorctl start program_name Start a process
    supervisorctl stop program_name Stop a process
    supervisorctl restart program_name Restart a process
    supervisorctl status program_name View the status of a process
    supervisorctl stop all Stop all processes\
    supervisorctl reload Load the latest configuration file and restart all processes
    supervisorctl update According to the latest configuration, restart the process whose configuration has been changed, and the process that has not been updated will not be affected
  3. echo_supervisord_conf

    It is used to generate the default configuration file (default configuration file). The content is very complete and annotated. It is suitable for reference when used. The usage is as follows

    echo_supervisord_conf > test.conf

Tags: Python

Posted by abazoskib on Sat, 16 Apr 2022 05:02:13 +0930