我有一个bash脚本来部署我的spring-boot应用程序(使用竹子)。
当spring-boot应用程序启动并运行时,脚本会挂起
java -jar myApp.jar我试着在后台运行它
java -jar myApp.jar &以及
java -jar myApp.jar &
disown只是"&“似乎什么也没做,而"&”后面跟着"disown“会使脚本失败。
当spring-boot应用程序继续运行时,我如何让脚本结束?
发布于 2016-04-29 05:42:42
有多个选项,其中一个是'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
start可以简单地启动进程,如下所示
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;这是在Linux上启动后台服务的适当方式。
发布于 2016-04-29 00:24:21
nohup java -jar myApp.jar &
nohup将在TTY关闭时截获HUP (挂起)信号。这样可以防止在用户注销/您的远程会话结束时终止进程。“&”符号用于在后台启动进程。
发布于 2019-09-23 09:05:20
轻松停止/启动spring boot应用程序uber jar https://github.com/tyrion9/spring-boot-startup-script
复制同一文件夹中的uber jar文件
./bootstrap.sh start
./bootstrap.sh stop
./bootstrap.sh restarthttps://stackoverflow.com/questions/36919196
复制相似问题