Supervisor User Manual

Supervisor introduction:

Supervisor ( http://supervisord.org ) is a process management tool written in Python that can be easily used to start, restart, and shut down processes (not just Python processes). In addition to controlling a single process, you can also start and shut down multiple processes at the same time. For example, unfortunately, a server problem causes all applications to be killed. At this time, you can use supervisor to start all applications at the same time instead of typing commands one by one. start up. Supervisor is a process management software under Linux. The two main functions are:
1) Turn non-daemon programs into demon mode to run, and daemon programs cannot be monitored.
2) Monitor the program, when the program exits, it can automatically pull up the program.

Install supervisor:

Supervisor is developed based on python. Before installing supervisor, you need to check whether the system has installed the python environment; if you install
it on ubuntu, you can use apt-get to install it directly:
#apt-get install supervisor
or you can use pip to install:
#pip install supervisor

Supervisor configuration example:

; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket
; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf

Basic use of Supervisor

Supervisor is quite powerful and provides a lot of functionality, but we may only need a small part of it.
After the installation is complete, you can write configuration files to meet your needs.
For convenience, we divide the configuration into two parts: supervisord (supervisor is a C/S model program, which is the server side, corresponding to the client side: supervisorctl) and the application (that is, the program we want to manage).
First look at the configuration file of the supervisor. After installing the supervisor , you can run the echo_supervisord_conf command to output the default configuration items, or redirect to a configuration file:
#echo_supervisord_conf > /etc/supervisor/supervisord.conf

The above is the initial configuration file of supervisord , in which we can configure the processes to be managed, but usually we should not do this, but write different programs (groups) to different programs (groups) by means of [include] in the configuration file. in the configuration file.
If there is a process called: tomcat , then we can create a new configuration file under /etc/supervisor/conf.d, tomcat.conf , and configure the process monitoring of tomcat in it.
Be sure to include your custom configuration file in your supervisord.conf configuration file, as follows:

[include]
files = /etc/supervisor/conf.d/*.conf

Let's specifically configure a process monitoring configuration file to start tomcat as a daemon:
#vim /etc/supervisor/conf.d/tomcat.conf

[program:tomcat]
command = /home/tomcat/bin/startup.sh
#启动程序的命令;
autostart = true
#在supervisord启动的时候也自动启动;
autorestart = true
#程序异常退出后自动重启;
startsecs = 5
#启动5秒后没有异常退出,就当作已经正常启动了;
startretries = 3
#启动失败自动重试次数,默认是3;
#user = tomcat
#开启进程使用哪个用户和组启动(这里tomcat启动时指定了nobody用户所以就不用再指定了);
redirect_stderr = true
#把stderr重定向到stdout,默认false;
stdout_logfile=/data/log/tomcat/out-memcache.log
#标准日志输出;
stderr_logfile=/data/log/tomcat/err-memcache.log
#错误日志输出;
stdout_logfile_maxbytes = 20MB
#标准日志文件大小,默认50MB;
stdout_logfile_backups = 20
#标准日志文件备份数;

A configuration file needs at least one configuration in the [program:x] section to tell supervisord which process needs to be managed.
The x in the [program:x] syntax represents the program name , which will be displayed on the client (supervisorctl or web interface). In supervisorctl , this value is used to start, restart, and stop the program .
The folder where the log files will be stored should be created. Another advantage of
using supervisor is that multiple processes can be started quickly. The configuration parameters are as follows:

process_name=%(process_num)s
numprocs=3

Indicates that three threads are opened for the same configuration.
Start Supervisor :
#service supervisor start
Or:
#/etc/init.d/supervisor start
You can also specify a configuration file to start:
#supervisord -c /etc/supervisor/supervisord.conf
After Supervisor is started, three files, supervisord.log, supervisord.pid, and supervisor.sock, will be generated in the /tmp directory. If there is a problem, you can view the log.
View the startup status:
# ps -ef| grep supervisor
root 10232 1 0 15:19 ? 00:00:00 /usr/bin/python /usr/bin/supervisord -c /etc/supervisor/supervisord.conf
View the process startup status: start, stop, and restart
#supervisorctl status
tomcat RUNNING pid 1230, uptime 0:01:12
the process :
#supervisorctl stop|start|restart tomcat

Supervisorctl command use:

Supervisors can be managed through the maintenance command supervisorctl or through the web management interface. The maintenance command supervisorctl can be used in two ways. One is imperative and the other is interactive.
Imperative:
1. Query the status of all processes:
#supervisorctl status
2. Start, stop and restart the business process, tomcat is the process name, that is , the value configured in [program:tomcat]
supervisorctl start tomcat
supervisorctl stop tomcat
supervisorctl restart tomcat
: 3. Manage all processes:
supervisorctl start all
supervisorctl stop all
supervisorctl restart all
4. Reload the configuration file and stop The original process and start all processes according to the new configuration (Note: all processes will be stopped and restarted, online operation is cautious)
supervisorctl reload
5. According to the latest configuration file, start the new configuration or the process with the change, the process with no configuration change will not Affected and restarted (Note: This is an online command that can be operated, and will not restart the original process)
supervisorctl update

Interactive:

# supervisorctl
tomcat                       RUNNING   pid 1256, uptime 0:01:47
supervisor> stop tomcat
tomcat: stopped
supervisor> start tomcat
tomcat: started
supervisor> status
tomcat                       RUNNING   pid 1258, uptime 0:00:04
supervisor> restart tomcat
tomcat: stopped
tomcat: started
supervisor> status
tomcat                       RUNNING   pid 1259, uptime 0:00:02
supervisor>

Summarize:

1) You can write your own script to add Supervisor to chkconfig and start automatically with the system. Or you can use an off-the-shelf script: Supervisor initscripts.
2) In addition to supervisorctl, you can also configure supervisorrod to start the web management interface. This web background uses Basic Auth for authentication.
3) In addition to the control of a single process, groups can also be configured for group management. Check log files frequently, including supervisord logs and log files of each pragram. Half of the program crash or exception information will be output to stderr. You can view the corresponding log files to find the problem.
4) Supervisor has a lot of functions, and there are many other configurations, you can get more information in the official documentation: http://supervisord.org/index.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324899956&siteId=291194637