diff --git a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java index 2ad08cac5b..57f44a7646 100644 --- a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java +++ b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/java/org/springframework/boot/launchscript/SysVinitLaunchScriptIT.java @@ -118,6 +118,14 @@ public class SysVinitLaunchScriptIT { .has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)")); } + @Test + public void forceStopWhenStopped() throws Exception { + String output = doTest("force-stop-when-stopped.sh"); + assertThat(output).contains("Status: 0"); + assertThat(output) + .has(coloredString(AnsiColor.YELLOW, "Not running (pidfile not found)")); + } + @Test public void startWhenStarted() throws Exception { String output = doTest("start-when-started.sh"); diff --git a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/force-stop-when-stopped.sh b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/force-stop-when-stopped.sh new file mode 100644 index 0000000000..465b555329 --- /dev/null +++ b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/force-stop-when-stopped.sh @@ -0,0 +1,4 @@ +source ./test-functions.sh +install_service +force_stop_service +echo "Status: $?" diff --git a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/test-functions.sh b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/test-functions.sh index aab41ca0f3..c1734c757b 100644 --- a/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/test-functions.sh +++ b/spring-boot-integration-tests/spring-boot-launch-script-tests/src/test/resources/scripts/test-functions.sh @@ -21,6 +21,10 @@ stop_service() { service spring-boot-app stop } +force_stop_service() { + service spring-boot-app force-stop +} + await_app() { if [ -z $1 ] then diff --git a/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script b/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script index feb9f538a7..cba5329854 100755 --- a/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script +++ b/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script @@ -203,6 +203,24 @@ do_stop() { return 1; } +force_stop() { + [[ -f $pid_file ]] || { echoYellow "Not running (pidfile not found)"; return 0; } + pid=$(cat "$pid_file") + isRunning "$pid" || { echoYellow "Not running (process ${pid}). Removing stale pid file."; rm -f "$pid_file"; return 0; } + do_force_stop "$pid" "$pid_file" +} + +do_force_stop() { + kill -9 "$1" &> /dev/null || { echoRed "Unable to kill process $1"; return 1; } + for i in $(seq 1 60); do + isRunning "$1" || { echoGreen "Stopped [$1]"; rm -f "$2"; return 0; } + [[ $i -eq 30 ]] && kill -9 "$1" &> /dev/null + sleep 1 + done + echoRed "Unable to kill process $1"; + return 1; +} + restart() { stop && start } @@ -242,6 +260,8 @@ start) start "$@"; exit $?;; stop) stop "$@"; exit $?;; +force-stop) + force_stop "$@"; exit $?;; restart) restart "$@"; exit $?;; force-reload)