brief introduction
Supervisor is a set of general process management program developed in Python. It can change an ordinary command-line process into a background daemon, monitor the process status, and automatically restart in case of abnormal exit. It starts these managed processes as subprocesses of supervisor through fork/exec, so as long as the path of the executable file of the process to be managed is written in the configuration file of supervisor. It also realizes that when the child process hangs, the parent process can accurately obtain the hanging information of the child process, and can choose whether to start and alarm by itself.
Supervisor also provides a function to set a non root user for supervisor or each child process, and this user can manage its corresponding process.
supervisor mainly includes the following four parts:
Supervisor: This is the main manager of the supervisor service, which is responsible for managing the sub processes we configure, including restarting the sub processes that crash or exit abnormally, and also responding to requests from clients.
supervisorctl: the client command line of the supervisord service. After listening to this, we can get the status of the sub processes controlled by the main process, stop and start the sub processes, and get the running list of the main process.
Web Server: and supervisorctl functions are beautiful. This is to view and control the process status through the web interface. XML-RPC
Interface: the same HTTP server serving the web UI provides an XML-RPC interface, which can be used to query and control the management program and its running program
Supervisor features
simple
Why is it simple? Because when we manage linux processes, we usually need to write a script that can realize the function of process start/stop/restart/reload, and then throw it to / etc / init D / below. There are many disadvantages in doing so. First, we have to write this script, which is very time-consuming and labor-consuming. Second, when the process hangs, linux will not restart it automatically. If we want to restart it automatically, we have to write a monitoring restart script ourselves. However, supervisor can solve these problems perfectly. OK, how to solve it? In fact, the supervisor management process is to start these managed processes as sub processes of the supervisor through fork/exec. In this case, we just need to write the path of the executable file of the process to be managed in the supervisor configuration file. This saves us the trouble of writing our own control script when we manage the process like linux. Second, the managed process is regarded as the child process of the supervisor. When the child process hangs, the parent process can accurately obtain the information of the child process hanging, so of course, the child process hanging can be restarted automatically. Of course, whether to restart or not depends on the fact that autostart=true is set in your configuration file, which will be discussed later
accurate
Why is it accurate? Because the feedback of linux on the process state is sometimes inaccurate. Why not? The landlord doesn't know. The official document says so. If you know, tell the landlord. Thank you very much. The supervisor monitors the subprocess and the status of the subprocess is undoubtedly accurate.
Process group
supervisor can uniformly manage the process group, that is, we can write the processes to be managed into a group, and then we manage the group as an object, such as start, stop, restart and so on. The linux system does not have this function. If we want to stop a process, we can only stop it one by one, or write a script to stop it in batch.
Centralized management
The process and process group information managed by supervisor are all written in an ini format file. Moreover, when we manage the supervisor, we can manage it locally or remotely. Moreover, the supervisor provides a web interface on which we can monitor and manage the process.
Of course, supervisor's XML needs to be called for local, remote and web management_ RPC interface, which will be discussed later.
Effectiveness
When the sub process of supervisor hangs up, the operating system will directly send a signal to supervisor. Other tools like supervisor send signals through the pid file of the process, and then poll regularly to restart the failed process. Obviously, supervisor is more efficient... As for the tools like supervisor, the landlord is not very clear. The landlord has also heard of God and director, but he hasn't used them. Interested friends can play
Scalability
Supervisor is an open source software, which can be changed directly. However, most of us should honestly study the interface provided by supervisor. Supervisor mainly provides two extensible functions. One is the event mechanism, which is what the landlord needs to do in the past two days. Another is XML_ RPC, the web management end of supervisor and remote call, we need to use it.
jurisdiction
Everyone knows that linux processes, especially those listening under port 1024, cannot be controlled by ordinary users in most cases. If you want to control, you must have root permission. Supervisor provides a function, which can set a non root user for supervisor or each child process, and this user can manage its corresponding process.
install
1.Debian/Ubuntu Can pass apt install apt-get install supervisor 2.other linux edition Step 1: yum install python-setuptools easy_install supervisor perhaps wget https://pypi.python.org/packages/source/s/supervisor/supervisor-3.1.3.tar.gz tar zxvf supervisor-4.2.2.tar.gz cd supervisor-4.2.2 python setup.py install For intranet installation package, see: http://10.10.10.105:4001/#group&do=file&gid=2&fid=323 Step 2 (or copy) http://10.10.10.105:4001 / #group & do = file & GID = 2 & FID = 323 / file in the supervisor directory to the specified location) Create a directory, mkdir /etc/supervisor Generate configuration files, echo_supervisord_conf > /etc/supervisor/supervisord.conf Step 3 start-up supervisord. supervisord -c /eyc/supervisor/supervisord.conf 3.View and install stone Buddha successfully echo_supervisord_conf
Configuration description
Supervisor is a C/S model program, supervisor is the server side, and supervisorctl is the client
End. supervisor configuration file: / etc / supervisor conf
Note: the configuration file of supervisor is incomplete by default, but in most cases, the basic functions mentioned above have been met by default.
Subprocess configuration file path: / etc / Supervisor D / Note: the default subprocess configuration file is in ini format, which can be modified in the supervisor main configuration file.
supervisor.conf configuration file description:
[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=0.0.0.0: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, in supervisorctl This value is used to perform a series of operations on the program [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 = /etc/supervisor/conf.d/*.ini ;You can specify one or more to.ini End profile,Of course, it can also be other types of files
Description of subprocess configuration file:
Write a configuration file for the sub process (program) to be managed and put it in / etc / Supervisor D / directory, with ini/.conf as extension:
;Project name [program:akcloud] ;Script directory,; Program startup directory directory=/opt/bin ;Script execution command command=java -jar /home/greatwall/sxhmak/akcloud/ak-cloud-services-pm-0.0.1-SNAPSHOT.jar TaskTestOne ; Monitored process path ;supervisor Whether to start at the same time when starting. Default True autostart=true ;When program exit When, this program No automatic restart,default unexpected,There are three options for setting the automatic restart after the subprocess hangs up, false,unexpected and true. If yes false In any case, it will not be restarted if it is unexpected,Only when the exit code of the process is not below exitcodes Defined in autorestart=true ;This option is the number of seconds after the child process starts. If the status is running,Then we think the startup is successful. The default value is 1 startsecs=1 ;User identity of the script run user = greatwall stderr_logfile=/home/greatwall/sxhmak/logs/akcloud_stderr.log ;Log output stdout_logfile=/home/greatwall/sxhmak/logs/akcloud_stdout.log ;hold stderr Redirect to stdout,default false redirect_stderr = true ;stdout Log file size, default 50 MB stdout_logfile_maxbytes = 20MB ;stdout Number of log file backups stdout_logfile_backups = 20 numprocs=1 ; Start several processes priority=1 ; The higher the number, the higher the priority startretries=10 ; Maximum number of retries when startup fails exitcodes=0 ; Normal exit code stopsignal=KILL ; A signal used to kill a process stopwaitsecs=10 ; send out SIGKILL Waiting time before supervisorctl Command description supervisorctl status //View the status of all processes supervisorctl stop program_name //Stop the specified program supervisorctl start program_name //Start the specified program supervisorctl restart program_name //Restart the specified program. 5. Supervisorctl update / / after modifying the configuration file, use this command to load a new configuration. 6. Supervisorctl reload / / restart all programs in the configuration. Note: Program_ Changing name to all can manage all processes in the configuration.
WEB page
Open supervisor Conf configuration file description: the contents marked in red can open the web interface; Note: " Comments in this file
It is not recommended to start the production environment, and the machine performance is not good. Access is shown in the figure:
Login interface (password is set):
Operation and maintenance interface
Power on
Applications not installed in the installation package will be automatically installed and started during installation,
Check whether the machine is started:
systemctl is-enabled supervisor wrong enabled You can't do it if you want to;
Create a new file supervisor.com in the directory / usr/lib/systemd/system / Service and add configuration content
[Unit] Description=Process Monitoring and Control Daemon After=rc-local.service nss-user-lookup.target [Service] Type=forking ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf ;Execute at startup ExecStop=/usr/bin/supervisord shutdown ExecReload=/usr/bin/supervisord reload killMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
Start service: systemctl enable Supervisor