修改pip源
```shell
mkdir ~/.pip
vim ~/.pip/pip.confweb
shell sudo pip install supervisor echo_supervisord_conf > /etc/supervisord.conf
去除裏面大部分註釋和「不相關」的部分,咱們能夠先看這些配置:
```shell
[unix_http_server]
file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 會使用
;chmod=0700 ; socket 文件的 mode,默認是 0700
;chown=nobody:nogroup ; socket 文件的 owner,格式: uid:gidshell
;[inet_http_server] ; HTTP 服務器,提供 web 管理界面
;port=127.0.0.1:9001 ; Web 管理後臺運行的 IP 和端口,若是開放到公網,須要注意安全性
;username=user ; 登陸管理後臺的用戶名
;password=123 ; 登陸管理後臺的密碼vim
[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 的方式鏈接 supervisordapp
咱們把上面這部分配置保存到 /etc/supervisord.conf(或其餘任意有權限訪問的文件),而後啓動 supervisord(經過 -c 選項指定配置文件路徑,若是不指定會按照這個順序查找配置文件:$CWD/supervisord.conf, $CWD/etc/supervisord.conf, /etc/supervisord.conf):
c supervisord -c /etc/supervisord.conf # 查看 supervisord 是否在運行: ps aux | grep supervisord
socket
shell [include] files = /etc/supervisor/*.conf
如今編寫一份配置文件來管理進程(須要注意:用 supervisord 管理時,gunicorn 的 daemon 選項須要設置爲 False):
```shell
[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.logui
; 能夠經過 environment 來添加須要的環境變量,一種常見的用法是修改 PYTHONPATH
; environment=PYTHONPATH=$PYTHONPATH:/path/to/somewhere
```url
使用 supervisorctl
```shell
supervisorctl -c /etc/supervisord.conf命令行
$ supervisorctl status $ supervisorctl stop usercenter $ supervisorctl start usercenter $ supervisorctl restart usercenter $ supervisorctl reread $ supervisorctl update ```