spring boot的jar包的启动,停止脚本

一、背景

spring boot的项目越来越多的打包成jar包来启动了。

二、脚本封装

脚本名为jarboot.sh,该脚本能启动该脚本通目录的spring boot的jar包文件

启动:jarboot.sh start

停止:jarboot.sh stop

重启:jarboot.sh restart

查看状态:jarboot.sh status

三、脚本源码

jarboot.sh为核心的启动脚本

#!/bin/bash
#
# chkconfig:   - 20 80
# description: Starts and stops the App.
# author:luoyong
 
source /etc/profile
#要启动java程序的用户
RUNNING_USER='bootuser';
#脚本所在的文件夹目录
APP_HOME=$(cd `dirname $0`; pwd)
#执行脚本的用户
WHO=`whoami`;
#可执行的jar的全路径,该文件夹下只能放置一个jar文件
JAR_FULL_NAME=`ls $APP_HOME/*.jar`;
#echo "JAR_FULL_NAME=$JAR_FULL_NAME";
#/apps/var/webapps/leopard-web/leopard-web.jar
#没有可执行的jar
if [ ! -n "${JAR_FULL_NAME}" ]; then
    echo "There is no executable jar file";
    exit 0;
fi
#有多个jar文件
#echo ${#JAR_FULL_NAME[*]};
if [ ${#JAR_FULL_NAME[*]} -gt 1 ];then
    echo "There is more than one executable jar file";
    exit 0;
fi
 
#可执行jar的名字
JAR_NAME=${JAR_FULL_NAME##*/};
#echo "JAR_NAME=$JAR_NAME";
#leopard-web.jar
 
ADATE=`date +%Y%m%d%H%M%S`;
#应用的名字,jar名字,除掉扩展名部分
APP_NAME=`echo $JAR_NAME|awk -F '.jar' '{print $1}'`;
#echo "APP_NAME=$APP_NAME";
#leopard-web
 
#配置jvm日志和gc日志路径
if [ ! -d "$APP_HOME/logs" ];then
    MKDIR_CMD="mkdir $APP_HOME/logs";
    if [[ $WHO == root ]];then
        su - $RUNNING_USER -c "$MKDIR_CMD";
    elif [[ $WHO == $RUNNING_USER ]];then
        eval $MKDIR_CMD;
    elif [[ $WHO != $RUNNING_USER ]];then
        su - $RUNNING_USER -c "$MKDIR_CMD";
    fi
fi
LOG_PATH=$APP_HOME/logs/nohup-$ADATE.out;
GC_LOG_PATH=$APP_HOME/logs/gc-$ADATE.log;
#JMX远程调试需要
#JMX="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=1091 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
#JVM参数
export JAVA_OPTS="$JAVA_OPTS -DdeployAppName=$APP_NAME \
-Duser.timezone=Asia/Shanghai \
-XX:+HeapDumpOnOutOfMemoryError \
-Xloggc:$GC_LOG_PATH -XX:+PrintGCDetails -XX:+PrintGCDateStamps \
-XX:NewRatio=1 -XX:SurvivorRatio=30 -XX:+UseParallelGC -XX:+UseParallelOldGC \
"
source $APP_HOME/setenv.sh;
#export JAVA_OPTS="$JAVA_OPTS \
#-Dcompany=shenyun \
#-Dspring.cloud.config.uri=http://springconf-test.shenyun.com \
#-Dspring.profiles.active=test \
#-Dspring.cloud.config.profile=test \
#-Xms512M -Xmx1024M \
#"
#该产生在1.8中已经去掉
# -XX:PermSize=256M -XX:MaxPermSize=512M
export JAVA_OPTS="$JAVA_OPTS \
-Dhmapp.jvm.log=$LOG_PATH \
-Dhmapp.logback.log=/apps/var/logs/$APP_NAME/info.log \
"
JAR_FILE=$APP_HOME/$JAR_NAME;
pid=0
start(){
  checkpid
  JAVA_CMD="nohup java -jar $JAVA_OPTS $JAR_FILE > $LOG_PATH 2>&1 &";
  if [ ! -n "$pid" ]; then
    if [[ $WHO == root ]];then
        su - $RUNNING_USER -c "$JAVA_CMD"
    elif [[ $WHO == $RUNNING_USER ]];then
        #nohup java -jar $JAVA_OPTS $JAR_FILE > $LOG_PATH 2>&1 &
        eval $JAVA_CMD;
    elif [[ $WHO != $RUNNING_USER ]];then
        su - $RUNNING_USER -c "$JAVA_CMD"
    fi
     
    startWaitSuccess
    echo "---------------------------------------"
    echo "application [${APP_NAME}] start success"
    echo "---------------------------------------"
  else
      echo "$APP_NAME is runing PID: $pid"  
  fi
}
startWaitSuccess(){
for((i=1;i<6;i++))
do
    if [ ${i} -lt 5 ];then
        sleep ${i}s;
    else
        sleep 5s;
    fi
    START_INFO=`ps -ef |grep $JAR_FILE |grep -v grep`
    if [ ! -n "$START_INFO" ];then
        echo "start ing .. $i...";
    else
        #不等于空
        echo "${START_INFO}";
        return;
    fi
done
}
status(){
   checkpid
   if [ ! -n "$pid" ]; then
     echo "$APP_NAME not runing";
   else
     echo "$APP_NAME runing PID: $pid";
   fi
}
checkpid(){
    pid=`ps -ef |grep $JAR_FILE |grep -v grep |awk '{print $2}'`
}
stop(){
    checkpid
    if [ ! -n "$pid" ];then
        echo "application status is stop"
        return;
   fi
   echo "stop ing ...";
    
   for i in $pid
   do
         echo "pid $i"
        `kill $i`
   done
   
for((i=1;i<5;i++))
do
    sleep ${i}s;
    checkpid
    if [ ! -n "$pid" ];then
        echo "application  stop success"
        return;
    else
        echo "stop ing .. $i..."
    fi
done
    
    
 #  echo "pids $pids";
   if [ ! -n "$pid" ];then
        echo "fffffff"
        echo "application status is stop"
   else
      for i in $pid
      do
         echo "pid $i"
        `kill -9 $i`
      done
      if [ $?==0 ];then
         echo "application force stop success"
      fi
   fi
}
restart(){
    stop
    start
}
case $1 in 
          start) start;; 
          stop)  stop;;
          restart)  restart;; 
          status)  status;;  
              *)  echo "require start|stop|restart|status"  ;; 
esac
#this shell's author is luoyong,if you want to use it,please keep this line;

 

setenv.sh为设置jvm的参数的文件

export JAVA_OPTS="$JAVA_OPTS \
-Dcompany=shenyun \
-Dspring.cloud.config.uri=http://springconf-test.shenyun.com \
-Dspring.profiles.active=test \
-Dspring.cloud.config.profile=test \
-Xms512M -Xmx1024M \
-Dserver.port=8080 \
"

 

你可能感兴趣的:(linux,JAVA)