Software and hardware environment
- centos7.6.1810 64bit
cat /etc/redhat-release #查看系统版本
-
supervisor 3.4.0
-
python 2.7.5
Supervisor introduction
supervisor
It is a tool python
written in language 进程管理
that can easily monitor, start, stop, and restart one or more processes. When a process is killed accidentally, supervisor
after monitoring the process death, the process can be easily restored automatically, no longer need programmers or system administrators to write code to control it.
supervisord installation
yum install -y epel-release
yum install -y supervisor
Start & turn on 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
supervisor's web side
supervisor
Provides web
control based on it . Administrators 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
vim /etc/supervisord.conf
If it is provided for external access, you need to change the port to the local ip address
#取消10-13行注释,前面数字是行号
[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))
Restart the service after the configuration is complete
systemctl restart supervisord
supervisord application configuration
Enter supervisord configuration file
cat /etc/supervisord.conf
Seen through the last line of the configuration file
[include]
files = supervisord.d/*.ini
In other words, all our application configuration files are saved in this directory, and they are saved in the format of **.ini**, you can modify the address by yourself, but do not modify the suffix
Let's create a monitored application
Create a test python configuration
Create an application configuration named python
vim /etc/supervisord.d/python.ini
The content of the configuration file, where command is the command that needs to be executed to start our application
[program:python] #这里的python就是我们显示在web前端以及终端的监控名称
command=python /tmp/supervisordtest/test.py #我们要监控的文件地址
autostart=true
autorestart=true
startsecs=1
startretries=3
redirect_stderr=true
stdout_logfile=/tmp/supervisordtest/access_python.log #日志地址,可自行配置目录
stderr_logfile=/tmp/supervisordtest/error_python.log #日志地址,可自行配置目录
Create test.py
mkdir /tmp/supervisordtest
vim /tmp/supervisordtest/test.py
Program content: open an endless loop, keep printing content
while True:
print(100)
Restart supervisord to make the configuration file take effect
systemctl restart supervisord
Check if the application starts normally
1. Command view
systemctl status supervisord
2. Visual web view
The web terminal can restart, stop, clean up the log, view the log and other operations
Several supervisor related commands
After installation, 3 system commands will be generated supervisorctl
, supervisord
andecho_supervisord_conf
-
supervisord
,supervisor
A processsupervisord
is started when it is running , it is responsible for starting the managed process, and starts the managed process as its own child process, and can automatically restart when the managed process crashes -
supervisorctl is a command line management tool that can be used to execute commands such as s tart stop restart to manage these child processes, such as
sudo supervisorctl start demoweb
Demoweb is the name of the process. For detailed commands and instructions, see the table below
command Description 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, restart all processes supervisorctl update According to the latest configuration, restart the process that has changed the configuration, and the process that has not been updated will not be affected -
echo_supervisord_conf
Used to generate the default configuration file (the default configuration file, the content is very complete and has comments, suitable for reference when used, the usage is like this
echo_supervisord_conf > test.conf