[TOC]
[**Supervisor**](http://supervisord.org) 是用 Python 開發的一套通用的進程管理程序,能將一個普通的命令行進程變為后臺 daemon,并監控進程狀態,異常退出時能自動重啟。它是通過 `fork/exec` 的方式把這些被管理的進程當作 supervisor 的子進程來啟動,這樣只要在 supervisor 的配置文件中,把要管理的進程的可執行文件的路徑寫進去即可。也實現當子進程掛掉的時候,父進程可以準確獲取子進程掛掉的信息的,可以選擇是否自己啟動和報警。
> 官網:http://supervisord.org
## **安裝**
Ubuntu:
~~~shell
apt install supervisor
~~~
CentOS:
~~~shell
yum install supervisor
~~~
## **配置文件**
### 創建配置文件
~~~shell
echo_supervisord_conf > /etc/supervisord.conf
~~~
_ps:需要 root 權限_
### [unix_http_server]
~~~text
file=/tmp/supervisor.sock ;UNIX socket 文件,supervisorctl 會使用
;chmod=0700 ;socket文件的mode,默認是0700
;chown=nobody:nogroup ;socket文件的所有者,格式:uid:gid
;username=user ; default is no username (open server)
;password=123 ; default is no password (open server)
~~~
> 修改 `file = /var/run/supervisor.sock`
### [supervisord]
~~~text
[supervisord]
logfile=/tmp/supervisord.log ;日志文件,默認是 $CWD/supervisord.log
logfile_maxbytes=50MB ;日志文件大小,超出會rotate,默認 50MB,如果設成0,表示不限制大小
logfile_backups=10 ;日志文件保留備份數量默認10,設為0表示不備份
loglevel=info ;日志級別,默認info,其它: debug,warn,trace
pidfile=/tmp/supervisord.pid ;pid 文件
nodaemon=false ;是否在前臺啟動,默認是false,即以 daemon 的方式啟動
minfds=1024 ;可以打開的文件描述符的最小值,默認 1024
minprocs=200 ;可以打開的進程數的最小值,默認 200
~~~
### [supervisorctl]
~~~shell
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
;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
~~~
### [program:xxxx]
~~~text
[program:theprogramname]
command=/bin/cat ; 命令路徑,如果使用python啟動的程序應該為 python /home/test.py
process_name=%(program_name)s ; 當`numprocs`為1時,process_name=%(program_name)s;當`numprocs` >= 2時,%(program\_name)s_%(process_num)02d
;numprocs=1 ; 啟動的進程數量 (def 1)
;directory=/tmp ; 執行目錄 (def no cwd) 若有 /home/supervisor\_test/test1.py,則`command` 只需設置成 python test1.py,否則`command`必須設置成絕對執行目錄
;umask=022 ; 權限 (default None)
;priority=999 ; 相對啟動優先級。值越高,最后啟動,最先被關閉 (default 999)
;autostart=true ; 如果是true,當supervisor啟動時,程序將會自動啟動 (default: true)
;startsecs=1 ; # 啟動延時執行 (def. 1)
;startretries=3 ; 啟動嘗試次數 (default 3)
;autorestart=unexpected ; 自動重啟 (def: unexpected)
;exitcodes=0 ; 與自動重啟一起使用的退出代碼。當退出碼是0,2時,執行重啟 (default 0)
;stopsignal=QUIT ; 停止信號 (default TERM)。中斷:INT(類似于Ctrl+C)(kill -INT pid),退出后會將寫文件或日志(推薦);終止:TERM(kill -TERM pid);掛起:HUP(kill -HUP pid),注意與Ctrl+Z/kill -stop pid不同;從容停止:QUIT(kill -QUIT pid);KILL, USR1, USR2其他見命令(kill -l),說明1
;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 ; 以root用戶執行
; 重定向
;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" ; 環境變量設置 (def no adds)
;serverurl=AUTO ; override serverurl computation (childutils)
~~~
詳細:http://supervisord.org/configuration.html#program-x-section-settings
### inet_http_server
可以使用瀏覽器查看和控制進程狀態
~~~shell
[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 ; 用戶名 (open server)
password=123 ; 密碼 (open server)
~~~
> 更多配置:http://supervisord.org/configuration.html
## **常用命令**
~~~shell
supervisord -c /etc/supervisord.conf # 啟動 supervisor ,如果不指定 `-c` 參數,則加載其安裝目錄下的 supervisord.conf 文件
supervisorctl status # 查看所有進程的狀態
supervisorctl start <process_name> # 啟動 一個進程
supervisorctl stop <process_name> # 停止一個進程,`<process_name>`:配置文件中定義的名稱
supervisorctl restart # 重啟,不會重新讀取配置文件
supervisorctl update # 重新加載配置并根據需要添加/刪除,并將重新啟動受影響的程序
supervisorctl reload # 重新啟動配置中的所有程序
supervisorctl reread # 重新加載守護進程的配置文件,不需要添加/刪除(不重啟)
~~~
> 官網提醒:
> 當**supervisord**啟動時,它將在默認位置搜索其配置文件,*包括當前工作目錄*。如果您有安全意識,您可能希望在**supervisord**命令指定配置文件的絕對路徑后指定一個“-c”參數,以確保有人不會欺騙您從包含流氓的目錄中運行 supervisorsupervisord.conf文件。當supervisor 以root 身份啟動而沒有這個\-c參數時,會發出警告。
## **管理 Laravel 隊列進程**
1. 修改 `/etc/supervisord.conf`
~~~text
[include]
files = /etc/supervisor/conf.d*.conf
~~~
2. 創建 `/etc/supervisor/conf.d/laravel-worker.conf`
~~~text
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /usr/share/nginx/html/tanteng.me/artisan queue:work --tries=3
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/var/log/supervisor/laravel-queue.log
~~~
> `user` 設置為 `root` 或當前運行命令的用戶
3. 啟動 supervisor / 重載配置
~~~shell
sudo supervisord -c /etc/supervisord.conf
~~~
如果已經啟動,則重載配置
~~~shell
sudo supervisorctl reread
~~~
4. 啟動隊列進程的監聽
~~~shell
sudo supervisorctl start laravel-worker:*
~~~
5. 查看啟動的進程
~~~shell
admin_s@SC-201811011347:/etc/supervisor$ ps aux |grep 'laravel'
root 108 0.1 0.3 88928 40332 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 109 0.1 0.3 88928 40388 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 110 0.1 0.3 88928 40476 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 111 0.1 0.3 88928 40528 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 112 0.1 0.3 88928 40572 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 113 0.1 0.3 88928 40572 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 114 0.1 0.3 88928 40320 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 115 0.1 0.3 88928 40428 ? S 08:53 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 193 1.6 0.3 88928 40552 ? S 09:06 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 194 1.3 0.3 88928 40500 ? S 09:06 0:00 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 195 1.8 0.3 88928 40584 ? S 09:06 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 196 1.6 0.3 88928 40644 ? S 09:06 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 197 1.3 0.3 88928 40424 ? S 09:06 0:00 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 198 1.7 0.3 88928 40780 ? S 09:06 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 199 1.6 0.3 88928 40696 ? S 09:06 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
root 200 2.0 0.3 88928 40624 ? S 09:06 0:01 php /mnt/d/phpStudy/PHPTutorial/WWW/laravel_workerman/artisan queue:work --tries=3
~~~
- PHP
- PHP 核心架構
- PHP 生命周期
- PHP-FPM 詳解
- PHP-FPM 配置優化
- PHP 命名空間和自動加載
- PHP 運行模式
- PHP 的 Buffer(緩沖區)
- php.ini 配置文件參數優化
- 常見面試題
- 常用函數
- 幾種排序算法
- PHP - 框架
- Laravel
- Laravel 生命周期
- ThinkPHP
- MySQL
- 常見問題
- MySQL 索引
- 事務
- 鎖機制
- Explain 使用分析
- MySQL 高性能優化規范
- UNION 與 UNION ALL
- MySQL報錯:sql_mode=only_full_group_by
- MySQL 默認的 sql_mode 詳解
- 正則表達式
- Redis
- Redis 知識
- 持久化
- 主從復制、哨兵、集群
- Redis 緩存擊穿、穿透、雪崩
- Redis 分布式鎖
- RedisBloom
- 網絡
- 計算機網絡模型
- TCP
- UDP
- HTTP
- HTTPS
- WebSocket
- 常見幾種網絡攻擊方式
- Nginx
- 狀態碼
- 配置文件
- Nginx 代理+負載均衡
- Nginx 緩存
- Nginx 優化
- Nginx 配置 SSL 證書
- Linux
- 常用命令
- Vim 常用操作命令
- Supervisor 進程管理
- CentOS與Ubuntu系統區別
- Java
- 消息隊列
- 運維
- RAID 磁盤陣列
- 邏輯分區管理 LVM
- 業務
- 標準通信接口設計
- 業務邏輯開發套路的三板斧
- 微信小程序登錄流程
- 7種Web實時消息推送方案
- 用戶簽到
- 用戶注冊-短信驗證碼
- SQLServer 刪除同一天用戶重復簽到
- 軟件研發完整流程
- 前端
- Redux
- 其他
- 百度云盤大文件下載
- 日常報錯記錄
- GIT
- SSL certificate problem: unable to get local issuer certificate
- NPM
- reason: connect ECONNREFUSED 127.0.0.1:31181
- SVN
- SVN客戶端無法連接SVN服務器,主機積極拒絕
- Python
- 基礎
- pyecharts圖表
- 對象
- 數據庫
- PySpark
- 多線程
- 正則
- Hadoop
- 概述
- HDFS