用于启动 spring-boot 应用程序并完成脚本的 Bash 脚本

swi*_*ers 2 deployment bash spring-boot

我有一个 bash 脚本来部署我的 spring-boot 应用程序(使用竹子)。

当 spring-boot 应用程序启动并正在运行时,脚本被挂起

java -jar myApp.jar
Run Code Online (Sandbox Code Playgroud)

我尝试在后台运行它

java -jar myApp.jar &
Run Code Online (Sandbox Code Playgroud)

java -jar myApp.jar &
disown
Run Code Online (Sandbox Code Playgroud)

只是“&”似乎什么都不做,而“&”后跟“disown”使脚本失败。

如何在 spring-boot 应用程序继续运行时让脚本完成?

小智 5

有多个选项,其中一个是“nohup”命令。另一种运行方式是使用“屏幕”虚拟终端。但我建议您采用更好的方法并将其作为 *nix 机器(如 apache、mysql 等)上的任何其他后台服务运行

这是我在/etc/init.d/great-spring-boot-app脚本中拥有的非常简单的代码,您可以编辑几行以适应您的约定,并将此文件保存在 /etc/init.d/ 目录中的任何名称中,例如/etc/init.d/my-cool-spring-boot-app

然后使其可执行: chmod +x /etc/init.d/my-cool-spring-boot-app

之后可以简单地通过执行类似的操作来启动进程

sudo service my-cool-spring-boot-app start

其他选项是:

stop|restart|status

#!/bin/bash -

#=-= START OF CUSTOM SERVICE CONFIGURATION =-#
# Where micro service war/jar file sits?
MS_HOME=/opt/MY_MICRO_SERVICE_ROOT_DIRECTORY # <--- EDIT THIS LINE

# Actual file name of Micro Service (jar or war), 
# ms-service.war or something-0.0.1-SNAPSHOT.jar, etc.
MS_JAR=MY_SPRING_BOOT_APPLICATION-0.0.1-SNAPSHOT.war # <--- EDIT THIS LINE
# ^^^ that should relative to MS_HOME directory.

# Which username we should run as.
RUNASUSER=USER_TO_RUN_AS; # <-- EDIT THIS LINE, 
# if port number for spring boot is < 1024 it needs root perm.

JAVA_HOME=/usr/local/jdk1.8.0_60; # <-- EDIT THIS, Where is your JDK/JRE?
PATH=${JAVA_HOME}/bin:${PATH};
SHUTDOWN_WAIT=20; # before issuing kill -9 on process.

export PATH JAVA_HOME


# These options are used when micro service is starting 
# Add whatever you want/need here... overrides application*.yml.
OPTIONS="
-Dserver.port=8080
-Dspring.profiles.active=dev
";
#=-= END OF CUSTOM CONFIGURATION =-=#

# Try to get PID of spring jar/war
MS_PID=`ps fax|grep java|grep "${MS_JAR}"|awk '{print $1}'`
export MS_PID;

# Function: run_as
run_as() {
    local iam iwant;

    iam=$(id -nu);
    iwant="$1";
    shift;

    if [ "${iam}" = "${iwant}" ]; then {
    eval $*;
    }
    else {
    /bin/su -p -s /bin/sh ${iwant} $*;
    } fi;
}

# Function: start
start() {
  pid=${MS_PID}
  if [ -n "${pid}" ]; then {
    echo "Micro service is already running (pid: ${pid})";
  }
  else {
    # Start screener ms
    echo "Starting micro service";
    cd $MS_HOME
    run_as ${RUNASUSER} java -jar ${OPTIONS} ./${MS_JAR};
    # java -jar ${OPTIONS} ./${MS_JAR}
  } fi;
  # return 0;
}

# Function: stop
stop() {
  pid=${MS_PID}
  if [ -n "${pid}" ]; then {

    run_as ${RUNASUSER} kill -TERM $pid

    echo -ne "Stopping micro service module";

    kwait=${SHUTDOWN_WAIT};

    count=0;
    while kill -0 ${pid} 2>/dev/null && [ ${count} -le ${kwait} ]; do {
      printf ".";
      sleep 1;
      (( count++ ));
    } done;

    echo;

    if [ ${count} -gt ${kwait} ]; then {
      printf "process is still running after %d seconds, killing process" \
    ${SHUTDOWN_WAIT};
      kill ${pid};
      sleep 3;

      # if it's still running use kill -9
      #
      if kill -0 ${pid} 2>/dev/null; then {
        echo "process is still running, using kill -9";
        kill -9 ${pid}
        sleep 3;
      } fi;
    } fi;

    if kill -0 ${pid} 2>/dev/null; then {
      echo "process is still running, I give up";
    } 
    else {
      # success, delete PID file, if you have used it with spring boot
      # rm -f ${SPRING_BOOT_APP_PID};
    } fi;
  } 
  else {
      echo "Micro service is not running";
  } fi;

  #return 0;
}

# Main Code

case $1 in
  start)
    start;
    ;;
  stop)
    stop;
    ;;
  restart)
    stop;
    sleep 1;
    start;
    ;;
  status)
    pid=$MS_PID
    if [ "${pid}" ]; then {
      echo "Micro service module is running with pid: ${pid}";
    }
    else {
      echo "Micro service module is not running";
    } fi;
    ;;
esac

exit 0;
Run Code Online (Sandbox Code Playgroud)

这是在 Linux 上启动后台服务的合适方法。