自定义程序启动脚本加入到supervisord下管理
ubuntu14.04 系统,直接通过apt-get安装即可
apt-get install supervisord
官网:http://www.supervisord.org/
主配置文件
这个配置在/etc/supervisor/supervisord.conf
配置内容如下:
root@xxx:/etc/supervisor/conf.d# grep -vE "(^;|^$)" /etc/supervisor/supervisord.conf
[unix_http_server]
file=/var/log/supervisord/supervisor.sock ; (the path to the socket file)
[supervisord]
logfile=/var/log/supervisord/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/var/log/supervisord/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:///var/log/supervisord/supervisor.sock ; use a unix:// URL for a unix socket
[include]
files = /etc/supervisor/conf.d/*.conf
应用配置文件
这个配置在/etc/supervisor/conf.d
root@xxx:/etc/supervisor/conf.d# cat C9019_app.conf
[program:C9019_app] # 这段要注意,改成你自己的
command=/opt/C9019/webapps/start.sh start # 启动脚本,有什么参数都给添加上,这个脚本是自己写的
directory=/opt/C9019/webapps # 在哪个目录下启动程序
autostart=true
exitcodes=0
autorestart=true
startretries=3
exitcodes=0,2
#stopsignal=TERM
#stopasgroup=true
stopwaitsecs=2
user=root
export JAVA_HOME=/opt/jdk1.8.0_121
export JRE_HOME=/opt/jdk1.8.0_121/jre
export PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin:$PATH
stdout_logfile=/var/log/supervisord/C9019_app_stdout.log
stderr_logfile=/var/log/supervisord/C9019_app_stderr.log
其他的参数请参考官网吧
启动
supervisord -c /etc/supervisor/supervisord.conf
自定义启动脚本如何加入到supervisord进行管理
首先我们看下自定义脚本内容:
#!/bin/bash
'''省略N行
function start(){
c="`ps -ef |grep config.type| grep -c ${MAIN_CLASS}`"
if [ $c -le 1 ]; then
echo "starting ${COMPONENT_NAME}..."
java xxx(省略N多内容) >$LOGDIR/error.log 2>&1 >$LOGDIR/error.log & # 注意看这个&号
echo $! > $LOGDIR/pid.log
echo "started ${COMPONENT_NAME} pid:$!"
else
echo "already started ${COMPONENT_NAME} pid:`cat $LOGDIR/pid.log`"
fi
}
function stop(){
echo "stoping ${COMPONENT_NAME}..."
for pid in $(ps aux|grep java|grep config.type| grep ${MAIN_CLASS}|awk '{print $2}')
do
echo "kill -9 ${pid}"
kill -9 ${pid}
done
echo "stopped ${COMPONENT_NAME}"
}
function restart(){
stop
sleep 10
start
}
case "${COMMAND}" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "require:start|stop|restart"
esac
exit 0
我之前自定义脚本在启动程序的时候是用了 & 让 程序后台运行的,这样就导致supervisord 无法掌控程序了,所以需要把& 给删除。
在有&符合启动的时候,产生如下错误,解决办法就是 java xxx(省略N多内容) >$LOGDIR/error.log 2>&1 >$LOGDIR/error.log &
最后一个&删掉,这样保持前台运行,supervisor就能掌控这个程序了。
root@xxx:/etc/supervisor/conf.d# supervisorctl
C9019_app FATAL Exited too quickly (process log may have details)
supervisor> restart C9019_app
C9019_app: ERROR (not running)
C9019_app: ERROR (abnormal termination
在去掉&以后,完美管控程序
root@xxx:/opt/C9019/webapps# supervisorctl
C9019_app RUNNING pid 14464, uptime 0:25:53
supervisor> status
C9019_app RUNNING pid 14464, uptime 0:25:54
supervisor> restart C9019_app
C9019_app: stopped
C9019_app: started
问题总结
无法管理程序
程序放入后台导致supervisor无法管理了,此时放入前台运行即可。