[TOC]
## 管理非daemon启动方式软件
>Supervisor([http://supervisord.org/](http://supervisord.org/))是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。
>因为Supervisor是Python开发的,安装前先检查一下系统否安装了Python2.4以上版本。下面以CentOS7,Python2.7版本环境下,介绍Supervisor的安装与配置步聚:
### 1、安装Python包管理工具([easy\_install](https://pypi.python.org/pypi/setuptools))
```
easy_install是setuptools包里带的一个命令,使用easy_install实际上是在调用setuptools来完成安装模块的工作,所以安装setuptools即可。
```
```
wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - | sudo python
```
### 2、安装supervisor
```
easy_install supervisor
```
```
supervisor安装完成后会生成三个执行程序:supervisortd、supervisorctl、echo_supervisord_conf,分别是supervisor的守护进程服务(用于接收进程管理命令)、客户端(用于和守护进程通信,发送管理进程的指令)、生成初始配置文件程序。
```
### 3、配置
>运行supervisord服务的时候,需要指定supervisor配置文件,如果没有显示指定,默认在以下目录查找:
```
$CWD/supervisord.conf
$CWD/etc/supervisord.conf
/etc/supervisord.conf
/etc/supervisor/supervisord.conf (since Supervisor 3.3.0)
../etc/supervisord.conf (Relative to the executable)
../supervisord.conf (Relative to the executable)
```
### 相关命令
```
# 生成配置文件
mkdir /etc/supervisor
echo_supervisord_conf > /etc/supervisor/supervisord.conf
# 启动
supervisord -c /etc/supervisor/supervisord.conf
# 客户端命令
* supervisorctl status 查看进程运行状态
* supervisorctl start 进程名 启动进程
* supervisorctl stop 进程名 关闭进程
* supervisorctl restart 进程名 重启进程
* supervisorctl update 重新载入配置文件
* supervisorctl shutdown 关闭supervisord
* supervisorctl clear 进程名 清空进程日志
* supervisorctl 进入到交互模式下。使用help查看所有命令。
* start stop restart + all 表示启动,关闭,重启所有进程。
* 重启相关所有进程
* supervisorctl status | grep "swoft-boy-accost-v1-order-create" | awk -F " " '{print $1}' | xargs supervisorctl restart
```
### 程序配置
```
[program:queue]
# 程序启动命令
command=php /mnt/hgfs/www/ndlot/think queue:listen
# 程序的启动目录
directory=/mnt/hgfs/www/ndlot
# 日志目录 需要注意当指定目录不存在时无法正常启动,所以需要手动创建目录(supervisord 会自动创建日志文件)
stdout_logfile=/mnt/hgfs/www/ndlot/queue.out
# 在 supervisord 启动的时候也自动启动
autostart=true
# 程序异常退出后自动重启
autorestart=true
# 进程启动优先级,默认999,值小的优先启动
priority=999
# 默认为false,进程被杀死时,是否向这个进程组发送stop信号,包括子进程
stopasgroup=true
# 默认为false,向进程组发送kill信号,包括子进程
killasgroup=true
[program:redis]
command=/www/server/redis/src/redis-server /www/server/redis/redis.conf
directory=/www/server/redis/
autorestart=true
startsecs=3
startretries=3
stdout_logfile=/www/server/panel/plugin/supervisor/log/redis.out.log
stderr_logfile=/www/server/panel/plugin/supervisor/log/redis.err.log
stdout_logfile_maxbytes=2MB
stderr_logfile_maxbytes=2MB
user=root
priority=999
numprocs=1
process_name=%(program_name)s_%(process_num)02d
```
### 一般常用配置
```
process\_name=%(program\_name)s #进程名称,默认是程序名称
command 启动程序命令
numprocs=1 #进程数量
directory=/tmp #路径
umask=022 #掩码
priority=999 #优先级,越大被开起的越早
autorestart=true #自动重启
startsecs=10 #启动等待时间(秒)
startretries=3 #启动重试次数
stopsignal=TERM #关闭信号
stopwaitsecs=10 #关闭前等待时间
user=chrism #监控用户权限
redirect_stderr=false #重定向报错输出
stdout_logfile=/a/path #输入重定向为日志
stdout_logfile_maxbytes=1MB #日志大小
stdout_logfile_backups=10 #日志备份
stdout_capture_maxbytes=1MB
stderr_logfile=/a/path
stderr_logfile_maxbytes=1MB
stderr_logfile_backups=10
stderr_capture_maxbytes=1MB
environment=A=1,B=2 #预定义环境变量
serverurl=AUTO #系统URL
```
### 配置文件
```
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
; - Shell expansion ("~" or "$HOME") is not supported. Environment
; variables can be expanded using this syntax: "%(ENV_HOME)s".
; - Quotes around values are not supported, except in the case of
; the environment= options as shown below.
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
; - Command will be truncated if it looks like a config file comment, e.g.
; "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
;
; Warning:
; Paths throughout this example file use /tmp because it is available on most
; systems. You will likely need to change these to locations more appropriate
; for your system. Some systems periodically delete older files in /tmp.
; Notably, if the socket file defined in the [unix_http_server] section below
; is deleted, supervisorctl will be unable to connect to supervisord.
# 指定了socket file的位置
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
;chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
; Security Warning:
; The inet HTTP server is not enabled by default. The inet HTTP server is
; enabled by uncommenting the [inet_http_server] section below. The inet
; HTTP server is intended for use within a trusted environment only. It
; should only be bound to localhost or only accessible from within an
; isolated, trusted network. The inet HTTP server does not support any
; form of encryption. The inet HTTP server does not use authentication
; by default (see the username= and password= options to add authentication).
; Never expose the inet HTTP server to the public internet.
# 用于启动一个含有前端的服务,可以从Web页面中管理服务。
;[inet_http_server] ; inet (TCP) server disabled by default
;port=127.0.0.1:9001 ; ip_address:port specifier, *:port for all iface
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
# 管理服务本身的配置
[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10 ; # of main logfile backups; 0 means none, default 10
loglevel=info ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false ; start in foreground if true; default false
silent=false ; no logs to stdout if true; default false
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
;umask=022 ; process file creation umask; default 022
;user=supervisord ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor ; supervisord identifier, default is 'supervisor'
;directory=/tmp ; default is not to cd during start
;nocleanup=true ; don't clean up tempfiles at start; default false
;childlogdir=/tmp ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value" ; key value pairs to add to environment
;strip_ansi=false ; strip ansi escape codes in logs; def. false
; The rpcinterface:supervisor 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:x] sections.
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
; The supervisorctl section configures how supervisorctl will connect to
; supervisord. configure it match the settings in either the unix_http_server
; or inet_http_server section.
# 客户端
[supervisorctl]
# 通过UNIX socket连接supervisord,路径与unix\_http\_server部分的file一致
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
# 通过HTTP的方式连接supervisord
;serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris ; should be same as in [*_http_server] if set
;password=123 ; should be same as in [*_http_server] if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available
; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.
# [program:xx]是被管理的进程配置参数,xx是进程的名称
;[program:theprogramname]
;command=/bin/cat ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=999 ; the relative start priority (default 999)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; when to restart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=true ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample eventlistener section below shows all possible eventlistener
; subsection values. Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.
# 对事件进行的管理
;[eventlistener:theeventlistenername]
;command=/bin/eventlistener ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1 ; number of processes copies to start (def 1)
;events=EVENT ; event notif. types to subscribe to (req'd)
;buffer_size=10 ; event buffer queue size (default 10)
;directory=/tmp ; directory to cwd to before exec (def no cwd)
;umask=022 ; umask for process (default None)
;priority=-1 ; the relative start priority (default -1)
;autostart=true ; start at supervisord start (default: true)
;startsecs=1 ; # of secs prog must stay up to be running (def. 1)
;startretries=3 ; max # of serial start failures when starting (default 3)
;autorestart=unexpected ; autorestart if exited after running (def: unexpected)
;exitcodes=0 ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT ; signal used to kill process (default TERM)
;stopwaitsecs=10 ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false ; send stop signal to the UNIX process group (default false)
;killasgroup=false ; SIGKILL the UNIX process group (def false)
;user=chrism ; setuid to this UNIX account to run the program
;redirect_stderr=false ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10 ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false ; emit events on stdout writes (default false)
;stdout_syslog=false ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10 ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false ; emit events on stderr writes (default false)
;stderr_syslog=false ; send stderr to syslog with process name (default false)
;environment=A="1",B="2" ; process environment additions
;serverurl=AUTO ; override serverurl computation (childutils)
; The sample group section below shows all possible group values. Create one
; or more 'real' group: sections to create "heterogeneous" process groups.
# 对任务组的管理 ,包含其它配置文件
;[group:thegroupname]
;programs=progname1,progname2 ; each refers to 'x' in [program:x] definitions
;priority=999 ; the relative start priority (default 999)
; 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.
# 可以指定一个或多个以.ini结束的配置文件
;[include]
;files = relative/directory/*.ini
```
- 简介
- PHP
- 字符串函数
- 数组函数
- 正则
- 加密函数
- 面向对象
- 关键字
- 设计模式
- 魔术方法
- 机制扩展
- 会话机制
- PHP框架
- laravel
- 问题
- swoole
- easyswoole
- workerman
- 数据库
- Sphinx
- MongoDB
- MemCache
- Redis
- 基础操作
- 数据类型
- 持久化
- 分布式锁
- 内存模型
- redis高级特性
- MySql
- 基础操作
- 数据类型
- 数据表引擎
- 锁机制
- 事务处理
- 存储过程
- 触发器
- 索引
- 关联查询
- 分析SQL语句-优化查询
- 分区分表
- 主从复制
- MySql安全性
- 网络协议
- HTTP
- header详解
- 状态码
- nginx-配置
- 逻辑算法
- 时间和空间复杂度
- 常见算法
- 数据结构
- 核心
- 进程、线程、协程
- 存储容量-计量单位
- 开发软件及配置
- 版本控制器
- Git
- Fidder
- Fidder-Android7
- 自动化部署
- Jenkins
- supervisor
- Elasticsearch
- LogStash
- RabbitMQ
- AB测试
- JAVA-JDK
- FileBeat
- PhpStorm
- Composer
- Linux
- API安全
- 高并发及大流量相关概念
- 网站优化
- WEB
- Electron