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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36919196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:34:23  来源:igfitidea点击:

Bash script to start spring-boot app and finish script

bashdeploymentspring-boot

提问by swinters

I have a bash script to deploy my spring-boot app (using bamboo).

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

script gets hung on this as the spring-boot app launches and is running

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

java -jar myApp.jar

I tried running it in the background with

我尝试在后台运行它

java -jar myApp.jar &

as well as

java -jar myApp.jar &
disown

just "&" seems to do nothing while the "&" followed by "disown" made the script fail.

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

How do I let the script finish while the spring-boot app keeps running?

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

回答by Jenya G

There is multiple options, one as mentioned is 'nohup' command. Another way to run is using 'screen' virtual terminal. But I would suggest you take a considerably better approach and run it as any other background service on *nix machines (like apache, mysql, etc.)

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

Here my very simple code that I have inside of /etc/init.d/great-spring-boot-appscript, you can edit few lines to suite your conventions and save this file with any name inside of /etc/init.d/ directory, for example /etc/init.d/my-cool-spring-boot-app

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

Then make it executable: chmod +x /etc/init.d/my-cool-spring-boot-app

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

Afterwards can simply start process by doing something like

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

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

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

Other options are:

其他选项是:

stop|restart|status

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 }'`
export MS_PID;

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

    iam=$(id -nu);
    iwant="";
    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  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;

This is the appropriate way to start background service(s) on Linux.

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

回答by joshiste

nohup java -jar myApp.jar &

nohup java -jar myApp.jar &

nohupwill intercept the HUP(hangup) signal when the TTY closes. This prevents the process from being terminated when the user logs out / your remote session ends. The ampersand is for starting the process in the background.

nohupHUP当 TTY 关闭时将拦截(挂断)信号。这可以防止在用户注销/您的远程会话结束时终止进程。&符号用于在后台启动进程。

回答by Neo Pham

Easy stop / start spring boot application uber jar https://github.com/tyrion9/spring-boot-startup-script

轻松停止/启动 spring boot 应用程序 uber jar https://github.com/tyrion9/spring-boot-startup-script

Copy uber jar file in the same folder

将 uber jar 文件复制到同一文件夹中

./bootstrap.sh start
./bootstrap.sh stop
./bootstrap.sh restart