[Turn] supervisord use

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 close 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.

Install

Supervisor can run on Linux, Mac OS X. As mentioned earlier, supervisor is written in Python, so it is also very easy to install, you can use pip directly:

sudo pip install supervisor

If it is an Ubuntu system, it can also be installed using apt-get.

supervisord configuration

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 supervisord configuration file. After installing supervisor, you can run echo_supervisord_conf the command to output the default configuration items, or redirect to a configuration file:

echo_supervisord_conf > /etc/supervisord.conf

Remove most of the comments and "irrelevant" parts inside, we can look at these configurations first:

[unix_http_server] file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 会使用 ;chmod=0700 ; socket 文件的 mode,默认是 0700 ;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gid ;[inet_http_server] ; HTTP 服务器,提供 web 管理界面 ;port=127.0.0.1:9001 ; Web 管理后台运行的 IP 和端口,如果开放到公网,需要注意安全性 ;username=user ; 登录管理后台的用户名 ;password=123 ; 登录管理后台的密码  [supervisord] logfile=/tmp/supervisord.log ; 日志文件,默认是 $CWD/supervisord.log logfile_maxbytes=50MB ; 日志文件大小,超出会 rotate,默认 50MB logfile_backups=10 ; 日志文件保留备份数量默认 10 loglevel=info ; 日志级别,默认 info,其它: debug,warn,trace pidfile=/tmp/supervisord.pid ; pid 文件 nodaemon=false ; 是否在前台启动,默认是 false,即以 daemon 的方式启动 minfds=1024 ; 可以打开的文件描述符的最小值,默认 1024 minprocs=200 ; 可以打开的进程数的最小值,默认 200 ; 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:///tmp/supervisor.sock ; 通过 UNIX socket 连接 supervisord,路径与 unix_http_server 部分的 file 一致 ;serverurl=http://127.0.0.1:9001 ; 通过 HTTP 的方式连接 supervisord ; 包含其他的配置文件 [include] files = relative/directory/*.ini ; 可以是 *.conf 或 *.ini 

We save the above part of the configuration to /etc/supervisord.conf (or any other file with permission to access), and then start supervisord (specify the configuration file path through the -c option, if not specified, the configuration file will be searched in this order: $ CWD/supervisord.conf, $CWD/etc/supervisord.conf, /etc/supervisord.conf):

supervisord -c /etc/supervisord.conf

Check if supervisord is running:

ps aux | grep supervisord

program configuration

Above we have got supervisorrod running, now we can add the configuration file of the process we want to manage. All configuration items can be written into supervisord.conf file, but this is not recommended. Instead, different programs (groups) can be written into different configuration files by means of include.

For example, we create a new directory /etc/supervisor/ to store these configuration files. Accordingly, modify the configuration of the include section in /etc/supervisord.conf:

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

Suppose there is a user center system written in Python and Flask framework, named usercenter, using gunicorn ( http://gunicorn.org/ ) as a web server. The project code is located  /home/leon/projects/usercenterin the gunicorn configuration file gunicorn.py, and the WSGI callable is the app property in wsgi.py. So the way to start directly from the command line may be like this:

cd /home/leon/projects/usercenter gunicorn -c gunicorn.py wsgi:app 

Now write a configuration file to manage this process ( note: when managing with supervisord, the gunicorn daemon option needs to be set to False ):

[program:usercenter] directory = /home/leon/projects/usercenter ; 程序的启动目录 command = gunicorn -c gunicorn.py wsgi:app ; 启动命令,可以看出与手动在命令行启动的命令是一样的 autostart = true ; 在 supervisord 启动的时候也自动启动 startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了 autorestart = true ; 程序异常退出后自动重启 startretries = 3 ; 启动失败自动重试次数,默认是 3 user = leon ; 用哪个用户启动 redirect_stderr = true ; 把 stderr 重定向到 stdout,默认 false stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB stdout_logfile_backups = 20 ; stdout 日志文件备份数 ; stdout 日志文件,需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件) stdout_logfile = /data/logs/usercenter_stdout.log ; 可以通过 environment 来添加需要的环境变量,一种常见的用法是修改 PYTHONPATH ; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere 

A configuration file needs at least one  [program:x] partial configuration to tell supervisord which process needs to be managed. [program:x] The program name in the syntax  x will be displayed on the client (supervisorctl or web interface). In supervisorctl, this value is used to start, restart, and stop the program.

A configuration example:

[program:api-cache-server]
command=python api_cache_server.py -c /root/sxadp-prod-conf
numprocs=1
directory=/root/api_trigger_script/
stdout_logfile=/var/log/api-cache-server/api-cache-server.log
stdout_logfile_maxbytes=10MB
stdout_logfile_backups=3
redirect_stderr=true
autostart=true
autorestart=true
user=root
stopasgroup=true

use supervisorctl

Supervisorctl is a command-line client tool of supervisord. When starting, you need to specify the same configuration file as supervisord. Otherwise, it will search for configuration files in the same order as supervisord.

supervisorctl -c /etc/supervisord.conf

The above command will enter the shell interface of supervisorctl, and then you can execute different commands:

> status    # 查看程序状态 > stop usercenter # 关闭 usercenter 程序 > start usercenter # 启动 usercenter 程序 > restart usercenter # 重启 usercenter 程序 > reread # 读取有更新(增加)的配置文件,不会启动新添加的程序 > update # 重启配置文件修改过的程序 

The above commands have corresponding output. In addition to entering the shell interface of supervisorctl, you can also run it directly in the bash terminal:

$ supervisorctl status
$ supervisorctl stop usercenter
$ supervisorctl start usercenter
$ supervisorctl restart usercenter
$ supervisorctl reread
$ supervisorctl update 

other

In addition to supervisorctl, you can also configure supervisorrod to start the web management interface. This web background uses Basic Auth for authentication.

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.

Supervisor has a lot of functions and many other configurations, you can get more information in the official documentation: http://supervisord.org/index.html

 

refer to:

http://liyangliang.me/posts/2015/06/using-supervisor/

Guess you like

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