简介
Supervisor 是一个用 Python 写的进程管理工具,可以很方便的用来在UNIX-like系统(不支持 Windows)下启动、重启、关闭进程。
安装
环境
centos 7
安装
安装supervisor
#!/bin/bash
install_supervisor()
{
if [ `command -v supervisord` ];then
return
fi
mkdir -p /var/logs/supervisord
yum install supervisor -y
sed -i 's/*.ini/*.conf/g' /etc/supervisord.conf
service supervisord restart
echo "/usr/bin/supervisord" >> /etc/rc.local
chmod +x /etc/rc.local
mkdir -p /etc/supervisor
}
install_supervisor
/etc/supervisor: 存放被supervisor管理的项目启动脚本
操作脚本
echo '[program:commercial-saas]
# 脚本目录
directory=/opt/messi/%(program_name)s/bin/
# 脚本执行命令
command=/opt/messi/%(program_name)s/bin/operate.sh restart
;numprocs=1
;process_name=%(program_name)s
user=root
autorestart=true
startsecs=30
killasgroup=true
stopasgroup=true
redirect_stderr=true
stdout_logfile = /var/logs/supervisord/%(program_name)s.log
loglevel=info' > /etc/supervisor/commercial-saas.conf
supervisorctl update
启动脚本
#!/bin/bash
# path of jar
BASE_PATH="/opt/bosco/commercial-saas"
# app name
APP_NAME="commercial-saas"
# jar name
JAR_NAME=$APP_NAME.jar
# app name
LOG_DIR=/var/logs/$APP_NAME
# jvm
JAVA_OPTS="-Xms1g -Xmx1g -XX:+AlwaysPreTouch -XX:+UseG1GC -XX:MaxGCPauseMillis=200"
JAVA_CMD="java $JAVA_OPTS"
JAVA_CMD=$JAVA_CMD" -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$LOG_DIR/$APP_NAME.heaperr.log.`date "+%Y%m%d%H%M%S"` -Xloggc:$LOG_DIR/$APP_NAME.gc.log.`date "+%Y%m%d%H%M%S"`"
JAVA_CMD=$JAVA_CMD" -jar $BASE_PATH/$JAR_NAME "
# stop method
stop(){
app_pid=`ps -ef | grep java | grep $APP_NAME | awk '{print $2}'`
if [[ $app_pid ]]; then
kill -9 $app_pid
echo "$APP_NAME was stopped"
else
echo "$APP_NAME has been already stopped"
fi
}
# start method
start(){
echo $JAVA_CMD
$JAVA_CMD
echo "$APP_NAME was started"
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "available operations: [start|stop|restart]"
exit 1
;;
esac
# start method
start(){
java $JAVA_OPTS -jar $BASE_PATH/$JAR_NAME
echo "$APP_NAME was started"
}
restart(){
stop
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo "available operations: [start|stop|restart]"
exit 1
;;
esac
备注
如果supervisor启动失败,可能是没有设置java 软连接
ln -s /opt/java/jdk1.8.0_301/bin/java /usr/bin/java
ln -s /opt/java/jdk1.8.0_301/bin/javac /usr/bin/javac