Original Address: https: //word.gw1770df.cc/2016-08-04/linux/supervisor-%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/
Supervisor
The official website for the http://supervisord.org/
A Process Control System
Use b / s architecture, running on Unix-like systems a process monitoring and management system
It can make the process run as a daemon, and has been monitoring process can automatically restart the process when unexpectedly quit.
installation
Supervisor is to use a pipe tool, install and use a python development process is very simple. Use pip
or easy_install
installation will be very convenient, automatically resolve dependencies.
- sudo pip install supervisor
- sudo easy_install supervisor
Please refer to pip install Python pip installation tutorial
Acquaintance
After the installation is complete, the system will increase the number of commands supervisord
used to start the service supervisor. supervisorctl
Services for managing supervisor, management supervisor use the process started.
After installation supervisor need to manually generate a configuration file. echo_supervisord_conf | sudo tee /etc/supervisord.conf
supervisor reads the default configuration file /etc/supervisord.conf
, we recommend the configuration files in this location.
Let's look at the simple supervisord.conf
, the same configuration file format and configuration ini file format by section, key, value composition, the configuration file comments for ;
.
[unix_http_server]
This section is connected to the profile supervisord the sock, the sock comprising a file path, the user authentication. Use thesupervisorctl
command to manage the process, use is this sock file. The default remains open, as this will shut[inet_http_server]
you can not use.[inet_http_server]
This section of the configurationsupervisor
of the web management interface and configure the listening address and port, the user has been authenticated. The default is disabled. To open the festival this comment removed, and then modify the appropriate key.[supervisord]
This section supervisor for a number of parameters to configure the service parameters, such as logs, log size and so on.[supervisorctl]
Supervisorctl configuration management program for parameters such as the file path sock, and requires[unix_http_server]
the same.[include]
This section contains a different profile role, we recommend that you enable this option, including additional configuration file, you will need to write the configuration management process additional profiles, often do not need to edit the main configuration file, to ensure safety. Modify the code
[include] files = /etc/supervisor.d/*.conf
Read /etc/supervisor.d/ folder suffix for all conf files.
first step
The first program uses a configuration management supervisord
We use python2.7 to demonstrate, using SimpleHTTPServer module easily and quickly start a http server.
1, create a profile /etc/supervisor.d/test1.conf, writes about the content
[program:simplehttpserver] command=/usr/bin/env python2.7 -m SimpleHTTPServer 20080
This is one of the most simple example, the first line specifies the name of the program currently configured. The second command line program run (http server port number is 20080).
2, the first program has been configured, you can then start to look supervisord effect.
Enter the command sudo supervisord
to start the service.
Many machines will print out a few lines of information, this does not mean the program error, meaning
"Supervisord is to use root to start, read the default location of the configuration file, if you need to read another location profiles use the -c parameter"
3, now use ps -ef | grep python
to check whether the configuration program just been launched. So far we have completed the first step.
Detailed Configuration
[Program: theprogramname] the Command = / bin / CAT; the program runs a command, it is recommended to use absolute paths. process_name =% (program_name) s; program name, available are variable `group_name`,` host_node_name`, `process_num` ,` program_name`, `here` ( profile directory). Situation in general need to run multiple copies of the program will be used. Later there will be examples. numprocs = 1; the number of copies of the program to run, as a default, if the value is greater than 1,% `process_name` must contain` (process_num) s` numprocs_start = 0; `% (process_num) s` starting number, default 0 00 = / tmp; the directory where the program is running, the equivalent of first cd to the specified directory, then run the program. = 022 the umask; Process for the umask (default None) priority = 999; operation procedure of priority, for example in the shutdown and restart will first start all / stop all, the high priority programs. autostart = true; supervisord starts automatically at startup, defaults to true startsecs =. 1; wait the waiting time before the program starts. The default is 1. startretries = 3; maximum number of attempts to restart. The default is 3. autorestart = unexpected; restart automatically, optional parameter is false, unexpected, true. If false not automatically restart, if unexpected signal showing if a program is not `exitcodes` exit, then restart automatically. The default is Unexpected exitcodes = 0,2; program exit code. With `autorestart` use. The default is 0,2 StopSignal = the QUIT; signal is sent to kill the process, the default is TREM. stopwaitsecs = 10; SIGKILL can wait before transmitting the signal. The default is 10. user; start the program in the specified user. The default is the current user. stopasgroup = false; whether the stop signal is sent to the child process, which is useful for Flask debug mode, if set to true, the stop signal is not sent to the child process. Default is false killasgroup = false; kill whether to send a signal to the child process, the default is false redirect_stderr is = false; the output directed to the standard error output, the default is false stdout_logfile = / a / path; log standard output path, an optional parameter of custom `` `AUTO`` NONE`, `` custom log write custom path, variables are available `group_name`,` host_node_name` , `process_num`,` program_name`, ` here` ( profile directory);` NONE` not create a log; `AUTO` and supervisord automatically select the path, and the original is automatically created when the service restarts supervisord log and the log backup the file will be deleted. The default is the AUTO stdout_logfile_maxbytes = 1MB; standard output log maximum single file size, if the log file exceeds the specified size will be backed up, the possible values KB MB GB. If set to 0 means no limit file size. The default is 50MB stdout_logfile_backups = 10; maximum number of backups standard output log file. The default is 10 stdout_capture_maxbytes = 1MB; when the write process is in "stdout capture mode" mode maximum number of bytes to a FIFO queue, the available units KB MB GB. The default is 0, described in detail, see [Capture-MODE] (http://supervisord.org/logging.html#capture-mode) stdout_events_enabled = to false; ; following configuration log Parameter configuration error output. The above standard output and the same configuration. = stderr_logfile / A / path; stderr_logfile_maxbytes = 1MB; = 10 stderr_logfile_backups; stderr_capture_maxbytes = 1MB; stderr_events_enabled = to false; Environment A = = ". 1", B = "2"; setting environment variables, variables available are `group_name`,` host_node_name`, `process_num` ,` program_name`, `here`. The default is empty. serverurl = AUTO; override serverurl computation ( childutils)
- Necessary parameters only
command
, not other optional parameters. [program:x]
A custom program name can not be repeated
Configuration Example
Application management tornado
4 need to start the process, four serial ports are listening (20081--20084).
The output is directed to the standard error output, the standard output port of each file written to a different, single file 50MB maximum, up to three backup storage.
Wait 3 seconds before the start.
start automatically when supervisord start. Run error automatically restart, the maximum number of attempts to restart three times.
[program:tornado-test] command=/usr/bin/env python2.7 /home/user/tornado-test.py --port=%(process_num)s process_name=%(program_name)s_port=%(process_num)s numprocs=4 numprocs_start=20081 redirect_stderr=true stdout_logfile=/tmp/tornado-test-%(process_num)s.log stdout_logfile_maxbytes=50MB stdout_logfile_backups=3 autorestart=true startsecs=3 startretries=3
Management nginx
nginx default state is running a program running in the background. supervisord can not manage such a program needs to modify nginx main configuration file.
Nginx join in the outermostdaemon off;
The simplest configuration.
[Program: Nginx] Command = / usr / sbin / Nginx; recommended absolute path.
supervisorctl
supervisor process management tools, you can easily start and stop the process, view the log, reload the configuration file.
If supervisord configuration file is no longer the default location, you need to specify the configuration file path using supervisorctl tool supervisordctl -c 配置文件路径
commonly used commands
supervisordctl status
View the process runningsupervisordctl start 进程名
Start the processsupervisordctl stop 进程名
Close processsupervisordctl restart 进程名
Restart the processsupervisordctl update
Reload the configuration filesupervisordctl shutdown
Close supervisordsupervisordctl clear 进程名
Empty process logsupervisordctl
Into interactive mode. Usehelp
all commands.
start
stop
restart
+ All expressed startup, shutdown, restart all processes.
If a process started more than one copy, such as described above tornado-test
.
Close all copies
Original Address: https: //word.gw1770df.cc/2016-08-04/linux/supervisor-%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B/
Supervisor
The official website for the http://supervisord.org/
A Process Control System
Use b / s architecture, running on Unix-like systems a process monitoring and management system
It can make the process run as a daemon, and has been monitoring process can automatically restart the process when unexpectedly quit.
installation
Supervisor is to use a pipe tool, install and use a python development process is very simple. Use pip
or easy_install
installation will be very convenient, automatically resolve dependencies.
- sudo pip install supervisor
- sudo easy_install supervisor
Please refer to pip install Python pip installation tutorial
Acquaintance
After the installation is complete, the system will increase the number of commands supervisord
used to start the service supervisor. supervisorctl
Services for managing supervisor, management supervisor use the process started.
After installation supervisor need to manually generate a configuration file. echo_supervisord_conf | sudo tee /etc/supervisord.conf
supervisor reads the default configuration file /etc/supervisord.conf
, we recommend the configuration files in this location.
Let's look at the simple supervisord.conf
, the same configuration file format and configuration ini file format by section, key, value composition, the configuration file comments for ;
.
[unix_http_server]
This section is connected to the profile supervisord the sock, the sock comprising a file path, the user authentication. Use thesupervisorctl
command to manage the process, use is this sock file. The default remains open, as this will shut[inet_http_server]
you can not use.[inet_http_server]
This section of the configurationsupervisor
of the web management interface and configure the listening address and port, the user has been authenticated. The default is disabled. To open the festival this comment removed, and then modify the appropriate key.[supervisord]
This section supervisor for a number of parameters to configure the service parameters, such as logs, log size and so on.[supervisorctl]
Supervisorctl configuration management program for parameters such as the file path sock, and requires[unix_http_server]
the same.[include]
This section contains a different profile role, we recommend that you enable this option, including additional configuration file, you will need to write the configuration management process additional profiles, often do not need to edit the main configuration file, to ensure safety. Modify the code
[include] files = /etc/supervisor.d/*.conf
Read /etc/supervisor.d/ folder suffix for all conf files.
first step
The first program uses a configuration management supervisord
We use python2.7 to demonstrate, using SimpleHTTPServer module easily and quickly start a http server.
1, create a profile /etc/supervisor.d/test1.conf, writes about the content
[program:simplehttpserver] command=/usr/bin/env python2.7 -m SimpleHTTPServer 20080
This is one of the most simple example, the first line specifies the name of the program currently configured. The second command line program run (http server port number is 20080).
2, the first program has been configured, you can then start to look supervisord effect.
Enter the command sudo supervisord
to start the service.
Many machines will print out a few lines of information, this does not mean the program error, meaning
"Supervisord is to use root to start, read the default location of the configuration file, if you need to read another location profiles use the -c parameter"
3, now use ps -ef | grep python
to check whether the configuration program just been launched. So far we have completed the first step.
Detailed Configuration
[program:theprogramname] command=/bin/cat ; 程序运行命令,建议使用绝对路径。 process_name=%(program_name)s ; 程序名称,可用的变量有 `group_name`, `host_node_name`, `process_num`, `program_name`, `here`(配置文件目录)。 一般程序需要运行多个副本的情况会使用。后面会有例子。 numprocs=1 ; 程序运行的副本个数,默认为1,如果值大于1,则`process_name` 必须包含 `%(process_num)s` numprocs_start=0 ; `%(process_num)s`起始数字,默认为0 00=/tmp ; 程序运行的所在目录,相当于先cd到指定目录,然后运行程序。 umask=022 ; umask for process (default None) priority=999 ; 程序操作的的优先级,例如在start all/stop all,高优先级的程序会先关闭和重启。 autostart=true ; 在supervisord启动时自动启动,默认为true startsecs=1 ; 程序启动前等待时间等待时间。默认为1。 startretries=3 ; 尝试重启最大次数。默认为3。 autorestart=unexpected ; 是否自动重启,可选参数为 false, unexpected, true。如果为false则不自动重启,如果为unexpected表示如果程序退出信号不在 `exitcodes` 中,则自动重启。默认为unexpected exitcodes=0,2 ; 程序退出码。配合`autorestart`使用。默认为 0,2 stopsignal=QUIT ; 杀死进程是发送的信号,默认为TREM。 stopwaitsecs=10 ; 发送SIGKILL信号前最大等待时间。默认为10。 user ; 以指定用户身份启动程序。默认为当前用户。 stopasgroup=false ; 是否向子进程发送停止信号,这对于Flask的debug模式很有用处,如果设置为true,则不向子进程发送停止信号。默认为false killasgroup=false ; 是否向子进程发送kill信号,默认为false redirect_stderr=false ; 将错误输出定向到标准输出,默认为false stdout_logfile=/a/path ; 标准输出日志路径,可选参数为 `自定义` `AUTO` `NONE`,`自定义`将日志写到自定义路径,可用的变量有`group_name`, `host_node_name`, `process_num`, `program_name`, `here`(配置文件目录);`NONE`不创建日志;`AUTO` 又supervisord自动选择路径,并且当supervisord服务重新启动时原来自动创建的日志以及日志的备份文件会被删除。默认为AUTO stdout_logfile_maxbytes=1MB ; 标准输出日志单个文件最大大小,如果超过指定大小会将日志文件备份,可用的单位 KB MB GB。如果设置为0则表示不限制文件大小。默认为50MB stdout_logfile_backups=10 ; 标准输出日志文件最大备份数。默认为10 stdout_capture_maxbytes=1MB ; 当进程处于“stdout capture mode”模式下写入到FIFO队列最大字节数,可用单位 KB MB GB。默认为0,详细说明见[capture-mode](http://supervisord.org/logging.html#capture-mode) stdout_events_enabled=false ; ;以下配置项配置错误输出的日志参数。和上面标准输出配置相同。 stderr_logfile=/a/path ; stderr_logfile_maxbytes=1MB ; stderr_logfile_backups=10 ; stderr_capture_maxbytes=1MB ; stderr_events_enabled=false ; environment=A="1",B="2" ; 环境变量设置,可用的变量有 `group_name`, `host_node_name`, `process_num`, `program_name`, `here`。 默认为空。 serverurl=AUTO ; override serverurl computation (childutils)
- 必要参数只有
command
,其他未可选参数。 [program:x]
自定义的程序名不能重复
配置示例
管理tornado应用
需要启动4个进程,分别监听四个连续端口(20081 – 20084)。
将错误输出定向到标准输出,每个端口标准输出写入到不同文件中,单个文件最大50MB,最多保存3个备份。
启动前等待3秒。
supervisord启动时自动启动。程序运行出错自动重启,最大尝试重启次数3次。
[program:tornado-test] command=/usr/bin/env python2.7 /home/user/tornado-test.py --port=%(process_num)s process_name=%(program_name)s_port=%(process_num)s numprocs=4 numprocs_start=20081 redirect_stderr=true stdout_logfile=/tmp/tornado-test-%(process_num)s.log stdout_logfile_maxbytes=50MB stdout_logfile_backups=3 autorestart=true startsecs=3 startretries=3
管理nginx
nginx默认运行状态是后台运行程序。supervisord不能管理此类程序需要,需要修改nginx主配置文件。
在nginx最外层加入daemon off;
最简单的配置。
[program:nginx] command=/usr/sbin/nginx ;建议使用绝对路径。
supervisorctl
supervisor进程管理工具,可以方便启动停止进程,查看程序日志,重新载入配置文件。
如果supervisord配置文件不再默认位置,使用supervisorctl工具时需要指定配置文件路径 supervisordctl -c 配置文件路径
常用命令
supervisordctl status
查看进程运行状态supervisordctl start 进程名
启动进程supervisordctl stop 进程名
关闭进程supervisordctl restart 进程名
重启进程supervisordctl update
重新载入配置文件supervisordctl shutdown
Close supervisordsupervisordctl clear 进程名
Empty process logsupervisordctl
Into interactive mode. Usehelp
all commands.
start
stop
restart
+ All expressed startup, shutdown, restart all processes.
If a process started more than one copy, such as described above tornado-test
.
Close all copies