Languages
[Edit]
EN

Bash - start / stop server script for Spring Boot Application

12 points
Created by:
AnnLen
13480

In this short article we would like to show how to write simple Bash script that runs own Spring Boot Application server under Linux.

That script lets user to run Spring Boot Application and close terminal safely - application will be working still.

Quick solution:

start-server.sh file:

#!/bin/bash
nohup java -jar /path/to/app.jar > /path/to/app.log 2>&1 &
echo $! > /path/to/app.pid

stop-server.sh file: 

#!/bin/bash
kill $(cat /path/to/app.pid)

Note: above solution doesn't deal with possible errors, read next section to see better solution.

 

Improved start / stop server scripts

The reasone why I have created this section in the article was to present some easy way to write start and stop server scripts with better messages on errors with prevention for some unexpected cases (pid file corrupted, port bussy etc.).

Scripts configuration steps:

  1. create in some directory 3 *.sh scrip files that were listed below
  2. indicate path to your applciation in server-config.sh,
  3. set permissions to execute scripts:
    chmod u+x server-config.sh start-server.sh stop-server.sh

Server start / stop operations can be done with:

./start-server.sh
./stop-server.sh

Script files

server-config.sh file:

#!/bin/bash

JAVA_HOME=`echo $JAVA_HOME`
APPLICATION_NAME=my-application-name
APPLICATION_PORT=443
APPLICATION_PATH="$SCRIPT_DIR/target/$APPLICATION_NAME.jar"
PID_PATH="$SCRIPT_DIR/target/$APPLICATION_NAME.pid"
OUTPUT_PATH="$SCRIPT_DIR/target/$APPLICATION_NAME.out"

Note: change file extension in APPLICATION_PATH from *.jar to *.war if it is necessary.

start-server.sh file:

#!/bin/bash

SCRIPT_PATH=`readlink -f "$0"`
SCRIPT_DIR=`dirname "$SCRIPT_PATH"`

. "$SCRIPT_DIR/server-config.sh"

if [ ! -f "$APPLICATION_PATH" ];
then
    echo "Incorrect application path '$APPLICATION_PATH'."
    exit 1
fi

if lsof -t "-i:$APPLICATION_PORT" -sTCP:LISTEN &> /dev/null;
then
    echo "Port $APPLICATION_PORTis in use.";
    exit 1
fi

if [ -f "$PID_PATH" ];
then
    PROCESS_PID=`cat "$PID_PATH"`

    if [ -n "$PROCESS_PID" ];
    then
        echo "$APPLICATION_NAME start operation error!"
        echo "Wait until previous start operation will be done or remove '$PID_PATH' file manually if you are sure the application is not starting."
        exit 1
    fi
fi

nohup "$JAVA_HOME/bin/java" -jar "$APPLICATION_PATH" "--server.port=$APPLICATION_PORT" > "$OUTPUT_PATH" 2>&1 &

if [ "$?" -eq 0 ];
then
    echo "$APPLICATION_NAME started!"
    echo "Monitor application output with: less +F '$OUTPUT_PATH'"
    echo "$!" > "$PID_PATH" || echo "Save PID $! in '$PID_PATH' file operation error."
    exit 0
else
    echo "$APPLICATION_NAME start operation error!"
    exit 1
fi

stop-server.sh file:

#!/bin/bash

SCRIPT_PATH=`readlink -f "$0"`
SCRIPT_DIR=`dirname "$SCRIPT_PATH"`

. "$SCRIPT_DIR/server-config.sh"

if [ -f "$PID_PATH" ];
then
    PROCESS_PID=`cat "$PID_PATH"`

    if [ -n "$PROCESS_PID" ];
    then
        if kill "$APPLICATION_NAME" &> /dev/null;
        then
            echo "$APPLICATION_PORT stopped!"
            rm -f "$PID_PATH" &> /dev/null || echo "" > "$PID_PATH"
            exit 0
        else
            echo "$APPLICATION_NAME stop operation error!"
            echo "Check process with PID $PROCESS_PID or remove '$PID_PATH' file manually."
            exit 1
        fi
    fi
fi

if lsof -t "-i:$APPLICATION_PORT" -sTCP:LISTEN &> /dev/null;
then
    echo "Kill process operation failed!";
    echo "It is necessary to kill process that uses port $APPLICATION_PORT manually.";
    echo "Use following command to find PID: lsof -t -i:$APPLICATION_PORT";
    exit 1
fi

Bash

Bash - start / stop server script for Spring Boot Application
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join