[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