一:簡介
supervisord是一個進程管理工具,提供web頁面管理,能對進程進行自動重啟等操作。
優點:
- 可以將非后台運行程序后台運行
- 自動監控,重啟進程
缺點:
- 不能管理后台運行程序
- 對多進程服務,不能使用kill關閉
二:安裝supervisord
1.pip安裝supervisord
pip install supervisor
2.生成配置文件
echo_supervisord_conf > /etc/supervisord.conf
如果報錯:
vim /usr/lib/python2.6/site-packages/supervisor-3.3.3-py2.6.egg-info/requires.txt
# 注銷如下內容即可
#meld3 >= 0.6.5
3.修改配置文件
[unix_http_server]
file=/tmp/supervisor.sock ; the path to the socket file
# 開啟web界面
[inet_http_server] ; inet (TCP) server disabled by default
port=mweb07:9001 ; ip_address:port specifier, *:port for all iface
username=admin ; default is no username (open server)
password=123456 ; 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
minfds=1024 ; min. avail startup file descriptors; default 1024
minprocs=200 ; min. avail process descriptors;default 200
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
[program:elasticsearch]
command=/data/elk/elasticsearch/bin/elasticsearch ; 管理命令,supervisor不支持后太進程
process_name=%(program_name)s
user=elk ;進程啟動用戶
autostart=true ;是否隨supervisor啟動
autorestart=true ;是否在掛了之后重啟,意外關閉后會重啟,比如kill掉!
startretries=3 ;啟動嘗試次數
stderr_logfile=/tmp/tail1.err.log ;標准輸出的位置
stdout_logfile=/tmp/tail1.out.log ;標准錯誤輸出的位置
loglevel=info ;日志的級別
[program:redis]
command=/data/elk/redis/src/redis-server /data/elk/redis/redis.conf
process_name=%(program_name)s
user=elk
directory=/data/elk/redi
4.啟動關閉
啟動:
supervisord -c /etc/supervisord.conf
關閉:
supervisorctl shutdown
管理命令:
supervisorctl stop program_name # 停止某一個進程,program_name 為 [program:x] 里的 x
supervisorctl start program_name # 啟動某個進程
supervisorctl restart program_name # 重啟某個進程
supervisorctl stop groupworker: # 結束所有屬於名為 groupworker 這個分組的進程 (start,restart 同理)
supervisorctl stop groupworker:name1 # 結束 groupworker:name1 這個進程 (start,restart 同理)
supervisorctl stop all # 停止全部進程,注:start、restartUnlinking stale socket /tmp/supervisor.sock
、stop 都不會載入最新的配置文件
supervisorctl reload # 載入最新的配置文件,停止原有進程並按新的配置啟動、管理所有進程
supervisorctl update # 根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
5.效果展示
三:安裝supervisord-monitor
- supervisord-monitor是對supervisord的一個集中化管理工具,可以對supervisor統一化操作
1.安裝
# 下載
git clone https://github.com/mlazarov/supervisord-monitor.git
# 生成配置文件
cd supervisord-monitor/
cp application/config/supervisor.php.example application/config/supervisor.php
2.修改配置文件,添加supervisord主機
mweb08 展示名 url 服務器地址 port 端口
$config['supervisor_servers'] = array(
'mweb08' => array(
'url' => 'http://mweb08/RPC2',
'port' => '9001',
'username' => 'admin',
'password' => '123456'
),
'mweb07' => array(
'url' => 'http://mweb07/RPC2',
'port' => '9001',
'username' => 'admin',
'password' => '123456'
),
);
3.添加nginx對supervisord-monitor的支持
server {
listen 82;
server_name localhost;
set $web_root /data/web/supervisord-monitor/public_html;
root $web_root;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $web_root$fastcgi_script_name;
fastcgi_param SCHEME $scheme;
}
}
5.展示,重啟nginx后,訪問即可