Differences between revisions 1 and 2
Revision 1 as of 2015-07-14 19:52:36
Size: 3579
Editor: twotwo
Comment:
Revision 2 as of 2019-08-03 17:30:54
Size: 4576
Editor: twotwo
Comment: upgrade to v4, support python3
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
||Back to [[Python#Python_based_Services]]||See Also [[systemd]]、[[pip]]|| ||Back to [[Python#Python_based_Services]]||See Also [[systemd]]、[[circus]]||
Line 15: Line 15:
 * 支持Python2.4以及之后的版本,但不支持Python3  * 支持Python2.4以及之后的版本,V4 已经很好的支持 Python3 了
Line 59: Line 59:
== Configuration File ==
== Use Case ==
Line 62: Line 63:
 * `stopsignal` TERM, HUP, INT, QUIT, KILL, USR1, or USR2. Default: TERM
Line 63: Line 65:
=== sshd === === within Docker ===
Line 65: Line 67:
;sshd,放在默认配置文件末尾
[program:sshd]
command=/usr/sbin/sshd -D
stdout_logfile=/var/log/supervisor/%(program_name)s.log
stderr_logfile=/var/log/supervisor/%(program_name)s.log
[unix_http_server]
file=/var/supervisor.sock

[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/var/supervisord.pid
nodaemon=true
minfds=1024

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/supervisor.sock ; use a unix:// URL for a unix socket

[program:daemon]
user=root
stopsignal=TERM
stopwaitsecs=5
command=python main.py
stdout_logfile=/var/log/%(program_name)s.log
stderr_logfile=/var/log/error.log
stderr_logfile_maxbytes=100MB ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=20 ; # of stderr logfile backups (default 10)
# redirect_stderr=true
autostart=true
startsecs=5
Line 71: Line 98:
startretries=3
Line 73: Line 101:
=== virtualenv example ===
[[http://stackoverflow.com/questions/6532486/supervising-virtualenv-django-app-via-supervisor|virtualenv config|target="_blank"]]
{{{#!highlight ini numbers=disable
FROM python:3.7-alpine as base
FROM base as builder
# Install Packages
# https://www.elastic.co/guide/en/beats/filebeat/7.2/filebeat-installation.html
RUN mkdir /install
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install" -r /requirements.txt
FROM base
# Copy Builder Image
COPY --from=builder /install /usr/local
Line 76: Line 114:
/etc/supervisord/example.ini RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
        apk add --update --no-cache libc6-compat tzdata &&\
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Line 78: Line 118:
{{{#!highlight ini numbers=disable
[program:example]
command=python /var/www/example/example.py --loglevel=%(ENV_LOGLEVEL)s
directory=/var/www/example
environment=PATH="/var/www/example/env/bin:%(ENV_PATH)s", my_home="/var/lib/example"
COPY supervisord.conf /etc/
WORKDIR /
CMD ["supervisord", "--configuration", "/etc/supervisord.conf"]
Line 89: Line 127:
 * [[||target="_blank"]]

Back to Python#Python_based_Services

See Also systemdcircus

Supervisor: A Process Control System

Supervisor是一个C/S系统,它可以在类UNIX系统上控制系统进程,由python编写,它提供了大量的功能来实现对进程的管理。

  1. 程序的多进程启动,可以配置同时启动的进程数,而不需要一个个启动
  2. 程序的退出码,可以根据程序的退出码来判断是否需要自动重启
  3. 程序所产生日志的处理
  4. 进程初始化的环境,包括目录,用户,umask,关闭进程所需要的信号等等
  5. 手动管理进程(开始,启动,重启,查看进程状态)的web界面,和xmlrpc接口

1. 支持的平台

  • 可以运行在大多数类UNIX系统上,比如Linux、MAC OS X、Solaris、FreeBSD
  • 不支持任何版本的Windows
  • 支持Python2.4以及之后的版本,V4 已经很好的支持 Python3 了

2. Quick Start

2.1. Installing via pip

http://supervisord.org/installing.html

pip install supervisor

2.2. Creating a Configuration File

# echo_supervisord_conf > /etc/supervisord.conf

...
; last line
[include]
files = /etc/supervisord/*.ini

2.3. Running supervisord

启动 http://supervisord.org/running.html#running-supervisord

  • # supervisors

  • 使用指定配置文件启动:supervisord -c supervisord.conf

2.4. Running supervisorctl

控制命令基本都通过supervisorctl执行,输入help可以看到命令列表。这是一些常用命令:

  • 获得所有程序状态 supervisorctl status
  • 关闭目标程序 supervisorctl stop spider
  • 启动目标程序 supervisorctl start spider
  • 关闭所有程序 supervisorctl shutdown

2.5. Running supervisord automatically on startup

https://github.com/Supervisor/initscripts

# wget https://raw.githubusercontent.com/Supervisor/initscripts/master/redhat-init-mingalevme -O /etc/init.d/supervisord
# chmod +x /etc/init.d/supervisord
# chkconfig --add supervisord
# chkconfig --level 2345 supervisord on
# chkconfig --list |grep supervisord
supervisord     0:off   1:off   2:on    3:on    4:on    5:on    6:off

PREFIX=/usr/local

3. Use Case

Configuration File

3.1. within Docker

[unix_http_server]
file=/var/supervisor.sock

[supervisord]
logfile=/var/log/supervisord.log
logfile_maxbytes=50MB
logfile_backups=10
loglevel=info
pidfile=/var/supervisord.pid
nodaemon=true
minfds=1024

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/supervisor.sock ; use a unix:// URL  for a unix socket

[program:daemon]
user=root
stopsignal=TERM
stopwaitsecs=5
command=python main.py
stdout_logfile=/var/log/%(program_name)s.log
stderr_logfile=/var/log/error.log
stderr_logfile_maxbytes=100MB   ; max # logfile bytes b4 rotation (default 50MB)
stderr_logfile_backups=20       ; # of stderr logfile backups (default 10)
# redirect_stderr=true
autostart=true
startsecs=5
autorestart=true
startretries=3

FROM python:3.7-alpine as base
FROM base as builder
# Install Packages
# https://www.elastic.co/guide/en/beats/filebeat/7.2/filebeat-installation.html
RUN mkdir /install
WORKDIR /install
COPY requirements.txt /requirements.txt
RUN pip install --install-option="--prefix=/install"  -r /requirements.txt
FROM base
# Copy Builder Image
COPY --from=builder /install /usr/local

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories  && \
        apk add --update --no-cache libc6-compat tzdata &&\
        ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

COPY supervisord.conf /etc/
WORKDIR /
CMD ["supervisord", "--configuration", "/etc/supervisord.conf"]

4. Reference

MainWiki: supervisor (last edited 2019-08-03 17:30:54 by twotwo)