springboot項目jar包啓動腳本

         當工具用習慣了。也就懶的去關心底層的東西了。項目部署的時候,用習慣了jenkins也就不關心運維寫的腳本了。但是當工具出問題,那就要從最基礎的腳本來部署項目。腳本我也不太懂,在這裏只是與大家一起分享啓動和關閉的兩個腳本,大神請繞路。

      注:springboot項目,jar包部署,main類啓動項目。腳本有很多,只要懂的原理,都能寫出來,僅供參考,沒有註釋,相關標籤知識自己補吧。

1.啓動腳本

#!/bin/sh
JAVA_OPTS=""
CLASSPATH=""
CATALINA_OPTS=""
PRG="$0"

RUN_USER="root"
PID_FILE="/data/sbc/項目名/tomcat.pid"

current_user=`whoami`
if [ "$current_user" != "$RUN_USER" ];then
  echo "please run $0 as user $RUN_USER" 1>&2
  exit 1
fi

# resolve links - $0 may be a softlink
while [ -h "$PRG" ]; do
  ls=`ls -ld "$PRG"`
  link=`expr "$ls" : '.*-> \(.*\)$'`
  if expr "$link" : '/.*' > /dev/null; then
    PRG="$link"
  else
    PRG=`dirname "$PRG"`/"$link"
  fi
done

# Get standard environment variables
PRGDIR=`dirname "$PRG"`
APP_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`

SPRINGBOOT_OUT="$APP_HOME"/log/springboot.out

# set JAVA_OPTS
ENV_CONF="$APP_HOME"/conf/env.conf
if [ -f $ENV_CONF ];then
   JAVA_OPTS=`sed 's/"//g' $ENV_CONF | awk '/^[^#]/' | tr "\n" ' '`
fi

# set CLASSPATH
if [ -d "$APP_HOME"/lib ];then
  for lib_jar in `ls "$APP_HOME/lib"`;do
     CLASSPATH="$APP_HOME"/lib/"$lib_jar":"$CLASSPATH"
  done
fi

# User can set environment in setenv.sh
if [ -r "$APP_HOME/bin/setenv.sh" ]; then
  . "$APP_HOME/bin/setenv.sh"
fi

echo "Using JAVA_HOME:  $JAVA_HOME"
echo "Using APP_HOME:   $APP_HOME"
if [ ! -z "$CLASSPATH" ];then
  echo "Using CLASSPATH:  $CLASSPATH"
fi

if [ ! -z "$PID_FILE" ]; then
  echo "Using PID_FILE:   $PID_FILE"
fi

if [ ! -z "$PID_FILE" ]; then
  if [ -f "$PID_FILE" ]; then
    if [ -s "$PID_FILE" ]; then
      echo "Existing PID file found during start."
      if [ -r "$PID_FILE" ]; then
        PID=`cat "$PID_FILE"`
        ps -p $PID >/dev/null 2>&1
        if [ $? -eq 0 ] ; then
          echo "Server appears to still be running with PID $PID. Start aborted."
          exit 0
        else
          echo "Removing/clearing stale PID file."
          rm -rf "$PID_FILE" >/dev/null 2>&1
          if [ $? != 0 ]; then
            if [ -w "$PID_FILE" ]; then
              cat /dev/null > "$PID_FILE"
            else
              echo "Unable to remove or clear stale PID file. Start aborted."
              exit 1
            fi
          fi
        fi
      else
        echo "Unable to read PID file. Start aborted."
        exit 1
      fi
    else
      rm -rf "$PID_FILE" >/dev/null 2>&1
      if [ $? != 0 ]; then
        if [ ! -w "$PID_FILE" ]; then
          echo "Unable to remove or write to empty PID file. Start aborted."
          exit 1
        fi
      fi
    fi
  fi
fi

# Add server.jar to classpath, if CLASSPATH is not blank, it must be endswith ":"
CLASSPATH="$CLASSPATH""$APP_HOME"/bin/server.jar

cat /dev/null > ${SPRINGBOOT_OUT}

${JAVA_HOME}/bin/java $JAVA_OPTS -classpath $CLASSPATH org.springframework.boot.loader.JarLauncher >> $SPRINGBOOT_OUT 2>&1 &
if [ ! -z "$PID_FILE" ]; then
  echo $! > "$PID_FILE"
fi

echo "springboot server started."

2.關閉腳本

 

#/bin/sh

RUN_USER="root"
PID_FILE="/data/sbc/項目名/tomcat.pid"
SHUTDOWN_PORT="8411"

current_user=`whoami`
if [ "$current_user" != "$RUN_USER" ];then
  echo "please run $0 as user $RUN_USER"
  exit 1
fi

if [ ! -f "$PID_FILE" ];then
  echo "pid file $PID_FILE not exist, please check if server running, stop aborted."
  exit 1
fi

netstat -an | grep LISTEN | grep -w $SHUTDOWN_PORT
if [[ $? -eq 0 ]]; then
  shut_info=`curl --connect-timeout 10 -m 30 -XPOST http://127.0.0.1:"$SHUTDOWN_PORT"/shutdown.json`
  echo "shutdown info: $shut_info ,result code: $?"
fi

pid=`cat $PID_FILE`
for num in 1 2 3 4 5 6 7 8 9 10
do
  echo "stop times: $num"
  still_alive=`ps -ef | awk '$2 ~ /^'$pid'$/{print $2}'`
  if [ ! -z "$still_alive" ];then
    kill $pid
    sleep 1
  else
    break
  fi
done

still_alive=`ps -ef | awk '$2 ~ /^'$pid'$/{print $2}'`
if [ ! -z "$still_alive" ];then
  kill -9 $pid
fi

rm -rf $PID_FILE
exit 0

再補充一種nohup java 方式的簡單啓動腳本

#!/bin/bash
#這裏可替換爲你自己的執行程序,其他代碼無需更改
APP_NAME=myApplication.jar
 
#使用說明,用來提示輸入參數
usage() {
    echo "Usage: sh start.sh [start|stop|restart|status]"
    exit 1
}
 
#檢查程序是否在運行
is_exist() { 
    pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}' `
    #如果不存在返回1,存在返回0
    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 $APP_NAME --server.port=83> /dev/null 2>&1 &
   fi
}
 
#停止方法
stop() {
   is_exist
   if [ $? -eq "0" ]; then
     kill -9 $pid
   else
     echo "${APP_NAME} is not running"
   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() {
   stop
   start
}
 
#根據輸入參數,選擇執行對應方法,不輸入則執行使用說明
case "$1" in
   "start")
     start
     ;;
   "stop")
     stop
     ;;
   "status")
     status
     ;;
   "restart")
     restart
     ;;
   *)
     usage
     ;;
esac

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章