superviosr是一個Linux/Unix系統上的進程監控工具,他/她upervisor是一個Python開發的通用的進程管理程序,能夠管理和監控Linux上面的進程,能將一個普通的命令行進程變爲後臺daemon,並監控進程狀態,異常退出時能自動重啓。不過同daemontools同樣,它不能監控daemon進程(也就是後臺進程)html
apt-get install -y supervisor
安裝成功後,會在/etc/supervisor
目錄下,生成supervisord.conf
配置文件。python
你也可使用echo_supervisord_conf > supervisord.conf
命令,生成默認的配置文件(不建議,內容比較多)。ios
nginxsupervisord.conf
示例配置:
; supervisor config file [unix_http_server] file=/var/run/supervisor.sock ; (the path to the socket file) chmod=0700 ; sockef file mode (default 0700) [supervisord] logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log) pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid) childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP) ; 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:///var/run/supervisor.sock ; use a unix:// URL for a unix socket ; 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. [include] files = /etc/supervisor/conf.d/*.conf
進程配置會讀取/etc/supervisor/conf.d
目錄下的*.conf
配置文件web
安裝完成以後,默認就啓動了supervisorubuntu
在上一篇文章中,連接以下:vim
http://www.javashuo.com/article/p-keoiahsf-o.htmlsocket
已經配置好了uwsgi和nginx。這是2個比較關鍵的進程,任意一個進程死掉,都會致使網頁沒法訪問。工具
關閉後臺運行,爲何呢?由於supervisord沒法管理後臺進程this
cd /www/mysite1/uwsgi
vim uwsgi.ini
註釋掉daemonize
[uwsgi] # Django-related settings # the base directory (full path) chdir = /www/mysite1 # Django's wsgi file module = mysite1.wsgi # the virtualenv (full path) home = /virtualenvs/venv # process-related settings # master master = true # maximum number of worker processes processes = 1 # pid file pidfile = /www/mysite1/uwsgi/uwsgi.pid # socket file path (full path) socket = /www/mysite1/uwsgi/mysite1.sock # clear environment on exit vacuum = true #The process runs in the background and types the log to the specified log file #daemonize = /www/mysite1/uwsgi/uwsgi.log
關閉uwsgi
/virtualenvs/venv/bin/uwsgi --stop uwsgi.pid
cd /etc/supervisor/conf.d
vim uwsgi.conf
內容以下:
[program:uwsgi] directory = /www/mysite1 ;程序的啓動目錄 command= /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini ;啓動命令 autostart = true ; 在 supervisord 啓動的時候也自動啓動 startsecs = 5 ; 啓動 5 秒後沒有異常退出,就看成已經正常啓動了 autorestart = true ; 程序異常退出後自動重啓 startretries = 3 ; 啓動失敗自動重試次數,默認是 3 user = root ; 用哪一個用戶啓動 redirect_stderr = true ; 把 stderr 重定向到 stdout,默認 false stdout_logfile_maxbytes = 20MB ; stdout 日誌文件大小,默認 50MB stdout_logfile_backups = 20 ; stdout 日誌文件備份數 ;stdout 日誌文件,須要注意當指定目錄不存在時沒法正常啓動,因此須要手動建立目錄(supervisord 會自動建立日誌文件) stdout_logfile = /www/mysite1/logs/stdout.log ;輸出的錯誤文件 stderr_logfile = /www/mysite1/logs/stderr.log ;添加運行須要的環境變量, 這裏用了虛擬環境 ;environment=PYTHONPATH=$PYTHONPATH:/virtualenvs/venv/bin/ ;而後確保殺死主進程後,子進程也能夠中止 stopasgroup=true killasgroup=true
建立日誌目錄
mkdir /www/mysite1/logs/
注意:supervisord不會自動幫你建立目錄,所以須要手動建立。
supervisorctl reload
若是出現:
error: <class 'socket.error'>, [Errno 2] No such file or directory: file: /usr/lib/python2.7/socket.py line: 228
先要確認進程是否存在:ps -ef | grep supervisord 而後使用命令 supervisord -c /etc/supervisor/supervisord.conf 啓動。
第一個查看,狀態爲STARTING,第二次查看時,狀態爲RUNNING
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status uwsgi STARTING root@ubuntu:/etc/supervisor/conf.d# supervisorctl status uwsgi RUNNING pid 20367, uptime 0:00:12
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi root 20367 0.3 0.8 102680 34832 ? S 13:50 0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini root 20369 0.0 0.7 102680 28768 ? S 13:50 0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini root 20377 0.0 0.0 15984 976 pts/1 S+ 13:52 0:00 grep --color=auto uwsgi root@ubuntu:/etc/supervisor/conf.d# killall -9 uwsgi root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep uwsgi root 20379 0.0 0.8 102676 34844 ? S 13:52 0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini root 20381 0.0 0.7 102676 28488 ? S 13:52 0:00 /virtualenvs/venv/bin/uwsgi --ini uwsgi/uwsgi.ini root 20383 0.0 0.0 15984 968 pts/1 S+ 13:52 0:00 grep --color=auto uwsgi
以上信息,能夠發現。強制kill掉進程以後,supervisor會將uwsgi啓動。
因爲supervisor不能監控後臺程序,
command = /usr/local/bin/nginx 這個命令默認是後臺啓動,
加上-g ‘daemon off;’這個參數可解決這問題,這個參數的意思是在前臺運行。
command = /usr/local/bin/nginx -g ‘daemon off;’
cd /etc/supervisor/conf.d
vim nginx.conf
內容以下:
[program:nginx] command = /usr/sbin/nginx -g 'daemon off;' startsecs=0 autostart=true autorestart=true stdout_logfile=/var/log/nginx/stdout.log stopasgroup=true killasgroup=true
加載配置
supervisorctl reload
查看狀態
root@ubuntu:/etc/supervisor/conf.d# supervisorctl status nginx RUNNING pid 20409, uptime 0:00:00 uwsgi RUNNING pid 20404, uptime 0:00:07
kill掉nginx進程
root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx root 20441 0.2 0.2 125116 9832 ? S 13:58 0:00 nginx: master process /usr/sbin/nginx -g daemon off; www-data 20442 0.0 0.0 125440 3228 ? S 13:58 0:00 nginx: worker process root 20446 0.0 0.0 15984 1084 pts/1 S+ 13:58 0:00 grep --color=auto nginx root@ubuntu:/etc/supervisor/conf.d# killall -9 nginx root@ubuntu:/etc/supervisor/conf.d# ps -aux|grep nginx root 20448 0.0 0.2 125116 9792 ? S 13:58 0:00 nginx: master process /usr/sbin/nginx -g daemon off; www-data 20449 0.0 0.0 125440 3132 ? S 13:58 0:00 nginx: worker process root 20451 0.0 0.0 15984 936 pts/1 S+ 13:58 0:00 grep --color=auto nginx
本文參考:
https://www.cnblogs.com/xishuai/p/ubuntu-install-supervisor.html