Skip to content

Commit ff681ad

Browse files
committed
Fix launch.script to not exit prematurely
Use "return" instead of "exit" where possible, especially in function definitions. Also fixed the exit codes to match the LSB spec for some specific conditions (fixes gh-3521). Fixes gh-3199, fixes gh-3535
1 parent 67483bb commit ff681ad

File tree

4 files changed

+28
-23
lines changed
  • spring-boot-docs/src/main/asciidoc
  • spring-boot-samples
  • spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools

4 files changed

+28
-23
lines changed

spring-boot-docs/src/main/asciidoc/deployment.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,9 @@ the default behavior in a script or on the command line:
432432
but if it is not a symlink, or you want to explicitly set the app name this can be
433433
useful.
434434

435+
|`RUN_ARGS`
436+
|The arguments to pass to the program (the Spring Boot app).
437+
435438
|`JAVA_HOME`
436439
|The location of the `java` executable is discovered by using the `PATH` by default, but
437440
you can set it explicitly if there is an executable file at `$JAVA_HOME/bin/java`.

spring-boot-samples/spring-boot-sample-actuator/pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@
6868
<plugin>
6969
<groupId>org.springframework.boot</groupId>
7070
<artifactId>spring-boot-maven-plugin</artifactId>
71+
<configuration>
72+
<executable>true</executable>
73+
</configuration>
7174
</plugin>
7275
</plugins>
7376
</build>

spring-boot-samples/spring-boot-sample-devtools/pom.xml

-3
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@
4343
<plugin>
4444
<groupId>org.springframework.boot</groupId>
4545
<artifactId>spring-boot-maven-plugin</artifactId>
46-
<configuration>
47-
<executable>false</executable>
48-
</configuration>
4946
</plugin>
5047
</plugins>
5148
</build>

spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script

+22-20
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ echoYellow() { echo $'\e[0;33m'$1$'\e[0m'; }
7171

7272
# Utility functions
7373
checkPermissions() {
74-
touch "$pid_file" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; exit 1; }
75-
touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; exit 1; }
74+
touch "$pid_file" &> /dev/null || { echoRed "Operation not permitted (cannot access pid file)"; return 4; }
75+
touch "$log_file" &> /dev/null || { echoRed "Operation not permitted (cannot access log file)"; return 4; }
7676
}
7777

7878
isRunning() {
@@ -115,40 +115,40 @@ command="$javaexe -jar -Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPT
115115
start() {
116116
if [[ -f "$pid_file" ]]; then
117117
pid=$(cat "$pid_file")
118-
isRunning $pid && { echoYellow "Already running [$pid]"; exit 0; }
118+
isRunning $pid && { echoYellow "Already running [$pid]"; return 0; }
119119
fi
120120
pushd $(dirname "$jarfile") > /dev/null
121121
if [[ -n "$run_user" ]]; then
122122
mkdir "$PID_FOLDER" &> /dev/null
123-
checkPermissions
123+
checkPermissions || return $?
124124
chown "$run_user" "$PID_FOLDER"
125125
chown "$run_user" "$pid_file"
126126
chown "$run_user" "$log_file"
127127
su -c "$command &> \"$log_file\" & echo \$!" $run_user > "$pid_file"
128128
pid=$(cat "$pid_file")
129129
else
130-
checkPermissions
130+
checkPermissions || return $?
131131
$command &> "$log_file" &
132132
pid=$!
133133
disown $pid
134134
echo "$pid" > "$pid_file"
135135
fi
136-
[[ -z $pid ]] && { echoRed "Failed to start"; exit 1; }
136+
[[ -z $pid ]] && { echoRed "Failed to start"; return 1; }
137137
echoGreen "Started [$pid]"
138138
}
139139

140140
stop() {
141-
[[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; exit 1; }
141+
[[ -f $pid_file ]] || { echoRed "Not running (pidfile not found)"; return 1; }
142142
pid=$(cat "$pid_file")
143143
rm -f "$pid_file"
144-
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; exit 1; }
145-
kill -HUP $pid &> /dev/null || { echoRed "Unable to kill process ${pid}"; exit 1; }
146-
for i in $(seq 1 20); do
147-
isRunning ${pid} || { echoGreen "Stopped [$pid]"; rm -f $pid_file; exit 0; }
144+
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; return 1; }
145+
kill -HUP $pid &> /dev/null || { echoRed "Unable to kill process ${pid}"; return 3; }
146+
for i in $(seq 1 60); do
147+
isRunning ${pid} || { echoGreen "Stopped [$pid]"; rm -f $pid_file; return 0; }
148148
sleep 1
149149
done
150150
echoRed "Unable to kill process ${pid}";
151-
exit 3;
151+
return 3;
152152
}
153153

154154
restart() {
@@ -157,31 +157,33 @@ restart() {
157157
}
158158

159159
status() {
160-
[[ -f $pid_file ]] || { echoRed "Not running"; exit 1; }
160+
[[ -f $pid_file ]] || { echoRed "Not running"; return 1; }
161161
pid=$(cat "$pid_file")
162-
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; exit 1; }
162+
isRunning $pid || { echoRed "Not running (process ${pid} not found)"; return 3; }
163163
echoGreen "Running [$pid]"
164-
exit 0
164+
return 0
165165
}
166166

167167
run() {
168168
pushd $(dirname "$jarfile") > /dev/null
169169
exec $command
170+
result = $?
170171
popd
172+
return $result
171173
}
172174

173175
# Call the appropriate action function
174176
case "$action" in
175177
start)
176-
start "$@";;
178+
start "$@"; exit $?;;
177179
stop)
178-
stop "$@";;
180+
stop "$@"; exit $?;;
179181
restart)
180-
restart "$@";;
182+
restart "$@"; exit $?;;
181183
status)
182-
status "$@";;
184+
status "$@"; exit $?;;
183185
run)
184-
run "$@";;
186+
run "$@"; exit $?;;
185187
*)
186188
echo "Usage: $0 {start|stop|restart|status|run}"; exit 1;
187189
esac

0 commit comments

Comments
 (0)