Linux 启动 springboot 脚本

使用姿势


##### 模板

# 使用 root 用户创建通用 springboot 模板并开启执行权限
su root
touch /usr/sbin/springboot
chmod a+x /usr/sbin/springboot
vi /usr/sbin/springboot
# 写入主脚本

##### 使用

# 脚本开启 root 用户不执行,使用项目绑定的用户 
# e.g. work
su work
# 这里我通常创建 bin release website recycle_bin logs docker shell src 几个目录
# bin 项目链接
# release 项目
# shell 项目启动脚本
# logs 项目日志记录
# src 源或项目源
cd ~/shell
# 创建编辑执行脚本 加入以下内容
vi gateway.sh
#!/bin/bash
source /usr/sbin/springboot
# source common/general-skywalking

# ------------------------------------------------
name="project-gate"

JAR_NAME="/home/work/bin/${name}"
LOG_PATH="/home/work/logs/${name}.log"
JVM_ARGUMENTS=(
    # "${skywalkingJVM[*]}"
    # "-Dskywalking.agent.service_name=${name}"
    "-Xms300M" 
    "-Xmx300M"
    # "-Dloader.path=/home/work/src/lib/gateway/"
)
# ------------------------------------------------

Application $1

# 给予执行脚本执行权限
chmod u+x gateway.sh
# 运行(无参或错参为帮助提示)
./gateway.sh

# 每次启动或重启会压缩备份项目日志到项目日志路径下的 backup 文件夹中

# 启动项目
./gateway.sh start

# 重启项目
./gateway.sh restart

# 停止项目
./gateway.sh kill

# 项目状态
./gateway.sh -status

# 项目日志备份
./gateway.sh logs

# 成功运行一次后再次登录(首次运行需要手动,运行帮助提示视为运行失败)
# ./gateway.sh 空格 两次[tab]查看命令提示
./gateway.sh [Tab][Tab]

# 重启所有项目
for var in $(grep "^source /usr/sbin/springboot" $(find . -type f) | tr ':' ' ' | awk '{print $1}'); do $var restart; done


脚本

#!/bin/bash

# **********************************************************
# * Author          : 夜路沏茶
# * Technical       : [email protected]
# * Filename        : springboot
# * Version         : 1.8.3
# * Description     : 启动 spring-boot 项目
# * -------------------------------------------------------
# * Last modified   : 2021-8-30 16:12:29
# * Last Update Log : 修复单个启动命令查找失败;
# * *******************************************************

LOG_BACKUP="backup"
EXECUTE=$0

Tips() {
  LogGroup "WARNING";
  echo "Usage: sh run.sh [start|stop|restart|status|logs]";
  echo "e.g. sh run.sh start";
  echo "";
  echo "    start: running a application of jar";
  echo "    restart: restart a application of jar";
  echo "    kill: stop a application of jar";
  echo "    -status: view status for a application of jar";
  echo "    logs: backup logs file";
  echo "";
  echo "Variables:";
  echo "    JAR_NAME: jar file";
  echo "    LOG_PATH: log path for springboot";
  echo "    JVM_ARGUMENTS: java jvm arguments ";
  echo "    ";
  exit 1;
}

GetName() {
  echo "$(cat ${LOG_PATH} | grep -P -o 'Started.*Application.*JVM running' | grep -P -o '.*Application' | awk '{print $NF}')";
}

JarPid() {
  echo $(ps -ef | grep $JAR_NAME | grep -v grep | awk '{print $2}');
}

Check() {
  if [ -z ${JAR_NAME} ]; then
    LogError "\$JAR_NAME is emtpy.";
    return 1;
  fi;
  if [ -z ${LOG_PATH} ]; then
    LogError "\$LOG_PATH is emtpy.";
    return 1;
  fi;
  pid=`JarPid`
}

Start() {
  Check;
  if [ -z $pid ]; then
    Logs;
    nohup java ${JVM_ARGUMENTS[*]} -jar "$JAR_NAME" > ${LOG_PATH} & pid=`JarPid`;
    echo ""
    echo "    nohup java ${JVM_ARGUMENTS[*]} -jar \"$JAR_NAME\" > ${LOG_PATH}"
    echo ""
    if [ ${operation} = "start" ]; then
      LogGroup "${pid}";
      LogInfo "Service ${JAR_NAME} is starting!pid=${pid}";
      LogInfo "Print Log (Start)";
      tail -f $LOG_PATH | sed -e "/.*JVM running.*/q";
      LogSuccess "$(GetName)";
    fi;
  else
    LogInfo "Service ${JAR_NAME} is already running, it's pid = ${pid}. If necessary, please use command: sh run.sh restart.";
  fi;
}

Stop() {
  Check;
  if [ -z $pid ]; then
    LogInfo "Service ${JAR_NAME} is not running! It's not necessary to stop it!";
  else
    kill -9 $pid;
    LogInfo "Service stop successfully!pid:${pid} which has been killed forcibly!";
  fi;
}

Status() {
  Check;
  if [ -z $pid ];then
    LogInfo "Service ${JAR_NAME} is not running!";
  else
    LogInfo "Service ${JAR_NAME} is running. It's pid=${pid}";
  fi;
}

Restart() {
  Check;
  LogGroup "Restart";
  if [ ! -z $pid ]; then
    kill -9 $pid;
  fi;
  Start;
  LogInfo "Print Log (Restart)";
  tail -f $LOG_PATH | sed -e "/.*JVM running.*/q";
  LogInfo "Restart $(GetName) Successfully";
}

Logs() {
  Check;
  if [ -z ${LOG_BACKUP} ]; then
    LogInfo "Please create backup folder for logs.";
    return 1;
  fi;
  LOG_FILE="$(echo ${LOG_PATH} | tr '/' ' ' | awk '{print $NF}')";
  LOG_TAR_FILE="${LOG_FILE}_$(date "+%Y_%m%d_%H%M%S").tar.gz";
  LOG_UPPER_PATH=$(echo "${LOG_PATH}" | sed "s/${LOG_FILE}//");
  if [ -z "$(ls ${LOG_UPPER_PATH} | grep ^${LOG_FILE}\$)" ]; then
    LogInfo "Skip backup logs.";
    return 5;
  fi;
  pwdEnthalpyToLogs=$(pwd)
  cd "${LOG_UPPER_PATH}"
  tar -zcvf "${LOG_TAR_FILE}" -C "${LOG_UPPER_PATH}" "${LOG_FILE}";
  mkdir -vp "${LOG_UPPER_PATH}/${LOG_BACKUP}";
  mv "${LOG_TAR_FILE}" "${LOG_UPPER_PATH}${LOG_BACKUP}";
  if [ $? = 0 ]; then
    LogInfo "Backup logs -> ${LOG_UPPER_PATH}${LOG_BACKUP}";
    cd "${pwdEnthalpyToLogs}";
  else
    LogError "\${LOG_TAR_FILE} -> ${LOG_TAR_FILE}";
    LogError "\${LOG_UPPER_PATH} -> ${LOG_UPPER_PATH}";
    LogError "\${LOG_FILE} -> ${LOG_FILE}";
    LogError "tar failed.";
    return 1;
  fi;
}

LogGroup() {
  line="----------------------------------------";
  echo ""; echo ""; echo ${line}"[ "$*" ]"${line};
  echo " - $(date "+%Y-%m-%d %H:%M:%S")"; echo "";
}

LogSuccess() {
  if [ -z "$1" ]; then LogInfo "Start Successfully";
  else LogInfo "Start <$1> Successfully"; fi;
}

LogInfo() {
  echo ""; echo " - [INFO] - $(date "+%Y-%m-%d %H:%M:%S") - $* "; echo "";
}

LogError() {
  echo ""; echo " - [ERROR] - $(date "+%Y-%m-%d %H:%M:%S") - $* "; echo "";
}

TabComplete() {
  cd "$(echo $EXECUTE | grep -P -o ".*/")"
  file=$(grep "/usr/sbin/springboot" $(find . -type f) -nl)
  # create fail when use '~'
  completeFile="/home/$(whoami)/.springboot-general-complete"

  if [ -z "$(grep "$completeFile"  ~/.bashrc | grep -v "^#")" ]; then
    LogInfo "Write AutoTabComplete"
    echo "" > $completeFile
    if [ ! $? = 0 ]; then exit 1; fi
    tee "$completeFile" <<-'EOF'
#!/bin/bash

SpringbootCompleteFunc() {
    COMPREPLY=()
    commandName="${COMP_WORDS[COMP_CWORD]}"
    completionTxt="start kill -status restart logs"
    COMPREPLY=($(compgen -W "${completionTxt}" -- ${commandName}))
    return 0
}

EOF
    if [ ! $? = 0 ]; then LogError "Write failed."; exit 1; fi
  fi

  for var in ${file[*]}; do
    if [ -z "$(grep "complete" $completeFile | grep -v "^#" | awk '{print $NF}' | grep "^$var$")" ]; then
      echo "complete -F SpringbootCompleteFunc $var" >> $completeFile
      echo "" >> $completeFile
    fi
  done

  if [ -z "$(grep "$completeFile"  ~/.bashrc | grep -v "^#")" ]; then
    LogInfo "Enable AutoTabComplete."
    LogInfo "Usage: ./run [Tab][Tab]"
    echo "if [ -f $completeFile ]; then" >> ~/.bashrc
    echo "        source $completeFile" >> ~/.bashrc
    echo "fi" >> ~/.bashrc
    echo ""; echo -e "\033[31m - COMMAND: \033[0m \033[5;31m source $completeFile\033[0m"; echo ""
  fi

}

Application() {
  if [ "$(whoami)" = "root" ]; then
    LogError "Prohibit starting projects with root."
    return 1
  fi;
  # LogInfo "From: $(pwd)"
  # LogInfo "User: $(whoami)"
  TabComplete
  operation=""
  case "$1" in
    "start") operation="start"; Start; ;;
    "kill") operation="stop"; Stop; ;;
    "-status") operation="status"; Status; ;;
    "restart") operation="restart"; Restart; ;;
    "logs") operation="logs"; Logs; ;;
    *) Tips; ;;
  esac
}

你可能感兴趣的:(spring,jar,linux,shell,spring,boot)