《利用 supervisor 輕松管理后臺進程》要點:
本文介紹了利用 supervisor 輕松管理后臺進程,希望對您有用。如果有疑問,可以聯系我們。
公司最近在做新的項目,前后端分離.前端改用node.js,后臺的接口都做成了類似的微服務,比如注冊一個服務,登錄一個服務等等···直接打包成jar包啟動,例如 java -jar xxx.jar
現在大概差不多有10多個左右的服務,今后會更多.
其實要放在后臺執行用nohup 命令 在把日志重定向也可以做, nohup java -jar xxx.jar > /data/log/xxx.log 2>&1 &
但是管理起來就很麻煩了,google 了一下發型supervisor 很適合做這個事情.網上找了很多關于supervisor 的資料都不怎么完整,所以這里把筆記貼一下,方便日后自己使用
下載最新 supervisor?
Centos 6.5 可以使用 yum 安裝,但是版本太老了.
我這里用的是?supervisor-3.1.3
可以到 supervisor 官網下載最新安裝包
http://supervisord.org/
下載鏈接?http://pan.baidu.com/s/1c0yf7Te
安裝?
tar zxvf supervisor-3.1.3
cd?supervisor-3.1.3
python setup.py install
配置
echo_supervisord_conf ?>/etc/supervisord.conf
vim /etc/init.d/supervisord
#! /bin/sh
# chkconfig: – 85 15
PATH=/sbin:/bin:/usr/sbin:/usr/bin
PROGNAME=supervisord
DAEMON=/usr/bin/$PROGNAME
CONFIG=/etc/$PROGNAME.conf
PIDFILE=/tmp/$PROGNAME.pid
DESC=”supervisord daemon”
SCRIPTNAME=/etc/init.d/$PROGNAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
start()
{
echo -n “Starting $DESC: $PROGNAME”
$DAEMON -c $CONFIG
echo “…”
}
stop()
{
echo -n “Stopping $DESC: $PROGNAME”
supervisor_pid=`ps -ef | grep supervisord | grep -v grep | awk ‘{print $2}’`
kill -15 $supervisor_pid
echo “…”
}
case “$1” in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo “Usage: $SCRIPTNAME {start|stop|restart}” >&2
exit 1
;;
esac
exit 0
chmod +x /etc/init.d/supervisord
chkconfig –add supervisord
chkconfig supervisord on
vim /etc/supervisord.conf
修改一下幾行,去掉前面的注釋“;”
[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)
chmod=0700 ; socket file mode (default 0700)
;chown=nobody:nogroup ; socket file uid:gid owner
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))
[inet_http_server] ; inet (TCP) server disabled by default
port=0.0.0.0:9001 ; (ip_address:port specifier, *:port for all iface)
username=user ; (default is no username (open server))
password=123 ; (default is no password (open server))
[include]
files = /etc/supervisor.conf/*.conf
我們寫一個測試的程序 hello.sh
vim /etc/supervisor.conf/hello.conf
[program:hello]
command= sh hello.sh
autostart=true ; supervisord守護程序啟動時自動啟動tornado
autorestart=true ; supervisord守護程序重啟時自動重啟tornado
redirect_stderr=true ; 將stderr重定向到stdout
user=root
directory=/www/sh ; cd 到應用目錄
stdout_logfile = /tmp/hello.log
寫一個測試腳本
vim /www/sh/hello.sh
#!/bin/bash
while true
do
echo “hello word”
sleep 10
done
chmox +x /www/sh/hello.sh
啟動并測試
/etc/init.d/supervisord start
查看端口是否監聽
lsof -i:9001
進入控制臺
supervisorctl
Server requires authentication
Username:user
Password:
hello RUNNING pid 12665, uptime 0:00:59
supervisor>
說明hello.sh 已經在后臺運行.
使用瀏覽器訪問9001端口,查看WEB界面
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/4559.html