1 2 3 4

Java启动脚本

#!/bin/bash
#判断是否输入了参数
#注意①
if [ $# != 3 ]; then
  usage
fi
#获取脚本名称
SCRIPT=$0
#获取操作符
JAR_NAME=$1
#获取操作符
OPERATOR=$2
#获取环境变量
ACTIVE=$3

#=-= <<开始>> 环境变量配置 =-=#
# JDK目录配置
JAVA_HOME=/usr/local/java/jre; # <-- 修改
PATH=${JAVA_HOME}/bin:${PATH};
export PATH JAVA_HOME

# JAR 文件所在目录
JAR_HOME=/releases/releases # <--- 修改

# JAR 应用文件名
APP_NAME=${JAR_NAME}.jar

# LOG目录
LOG_PATH=/releases/logs/${JAR_NAME}

# JAR 启动参数
# Add whatever you want/need here... overrides application*.yml.
OPTIONS="
-Dspring.profiles.active=${ACTIVE}
-Dlogging.path=${LOG_PATH}
-Xms1024m -Xmx1024m -XX:MaxNewSize=512m -XX:SurvivorRatio=6
"; # <--- 按需要添加或修改

#=-= <<结束>> 环境变量配置 =-=#
#建议使用 . xx.sh 命令执行脚本。如果使用sh xx.sh执行,注意is_exist方法里的注释

usage() {
  echo "Usage: sh $SCRIPT [jar package name] [start|stop|restart|status] [dev|test|pro]"
  exit 1
}

if [ ! -d ${LOG_PATH} ]; then
  mkdir ${LOG_PATH};
fi

is_exist(){
  #过滤grep命令本身
    #注意②
  #pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
  #使用sh xx.sh命令执行的话,启用下面代码
  pid=`ps -ef|grep ${APP_NAME}|grep -v grep|grep -v ${SCRIPT}|awk '{print $2}' `
  if [ -z "${pid}" ]; then
    return 1
  else
    return 0
  fi
}

start(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is already running. pid=${pid} ."
  else
    nohup java -jar ${OPTIONS} ${JAR_HOME}/${APP_NAME} > /dev/null 2>&1 &
    echo "${JAR_NAME} 开始启动..."
    s_int=5
    sleep ${s_int}
    timer=240 #120s定时器
    while [ $timer -gt 0 ]
    do
      APP_PID=`ps -ef|grep ${APP_NAME}|grep -v grep|grep -v ${SCRIPT}|awk '{print $2}' `
      if [[ ! ${APP_PID} ]]; then
        echo "${JAR_NAME} 启动中...";
        sleep ${s_int};
        let timer-=${s_int};
      else
        APP_PORTS=`netstat -nap | grep ${APP_PID} | grep LISTEN | awk '{print $4}' | awk -F: '{print $2}'`;
        if [[ ! ${APP_PORTS} ]]; then
          echo "${JAR_NAME} 启动中...";
          sleep ${s_int};
          let timer-=${s_int};
        else
          for p in ${APP_PORTS}; do
            APP_STATUS=`curl -s http://localhost:${p}/health | grep "UP"`
            if [[ APP_STATUS ]]; then
              echo "${JAR_NAME} 启动成功!状态UP 端口${p}";
              break;
            fi
          done
          if [[ APP_STATUS ]]; then
            break;
          else
            sleep ${s_int};
            let timer-=${s_int};
          fi
        fi
        #statements
      fi
    done
  fi
}

stop(){
  is_exist
  if [ $? -eq "0" ]; then
    #下线应用
    echo "${JAR_NAME} 下线中...${APP_DOWN}";
    kill -15 ${pid}
    sleep 5
    is_exist
    if [ $? -eq "0" ]; then
      kill -9 ${pid}
    fi
  else
    echo "${APP_NAME} is not running"
    #exit 0
  fi
}

status(){
  is_exist
  if [ $? -eq "0" ]; then
    echo "${APP_NAME} is running. Pid is ${pid}"
  else
    echo "${APP_NAME} is NOT running."
  fi
}

restart(){
  is_exist
  stop
  start
}

case "$OPERATOR" in
  "start")
start ;;
"stop")
stop ;;
"status")
status ;;
"restart")
restart ;;
*)
usage ;;
esac
posted @ 2021-05-11 10:46  请叫我阿鑫  阅读(444)  评论(0编辑  收藏  举报