Supervisor是一個用 Python 寫的進程管理工具,能夠很方便的用來啓動、重啓、關閉進程.centos
pip install supervisor
cho_supervisord_conf > /etc/supervisord.conf
[unix_http_server] file=/tmp/supervisor.sock ; UNIX socket 文件,supervisorctl 會使用 [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 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface [supervisorctl] serverurl=unix:///tmp/supervisor.sock ; 經過 UNIX socket 鏈接 supervisord,路徑與 unix_http_server 部分的 file 一致 [include] files = relative/directory/*.ini ; 能夠是 *.conf 或 *.ini
[program:appname] directory = /myproject ; 程序的啓動目錄 command = gunicorn -w 8 -b 0.0.0.0:9090 manage:app ; 啓動命令 autostart = true ; 在 supervisord 啓動的時候也自動啓動 startsecs = 5 ; 啓動 5 秒後沒有異常退出,就看成已經正常啓動了 autorestart = true ; 程序異常退出後自動重啓 startretries = 3 ; 啓動失敗自動重試次數,默認是 3 user = www ; 用哪一個用戶啓動 redirect_stderr = true ; 把 stderr 重定向到 stdout,默認 false stdout_logfile_maxbytes = 50MB ; stdout 日誌文件大小,默認 50MB stdout_logfile_backups = 20 ; stdout 日誌文件備份數 stdout_logfile = /var/log/appname.log
其中 [program:appname] 中的 appname 是應用程序的惟一標識,不能重複。對該程序的全部操做(start, restart 等)都經過名字來實現。
幾個注意點:
1.執行命令必須是絕對路徑的命令
2.執行程序必須是前臺運行,若是是後臺運行的轉爲前臺
3.若是涉及子進程添加如下參數,確保子進程都能中止app
stopasgroup=true killasgroup=true
supervisord -c /etc/supervisord.conf
(1).新建supervisord.service
文件socket
#supervisord.service [Unit] Description=Supervisor daemon [Service] Type=forking ExecStart=/bin/supervisord -c /etc/supervisord.conf ExecStop=/bin/supervisorctl shutdown ExecReload=/bin/supervisorctl reload KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
(2).添加爲服務並開機啓動工具
cp supervisord.service /usr/lib/systemd/system/ service supervisord start chkconfig supervisord on
supervisorctl status supervisorctl stop appname supervisorctl start appname supervisorctl restart appname supervisorctl reread supervisorctl update