Running Spring Boot, Part II

This post will step through setting up a startup script for a spring boot application on Linux. This has been done before, but I think this script is an improvement.

In the previous post we saw how to use boot’s ApplicationPidListener to manage a pid file. Now we will use a script in conjunction with this pid file to manage the running of our application.

There’s not much to see here, it’s just a bash script that uses the pid file to check for the process running or to stop the process. Possible command line options include start, stop, restart, debug, and status. So for example, we could use “app.sh start” and “app.sh stop” to stop the program. A possible future enhancement could be to pass the jar file as an argument, then the specific file name is not tied to this script and it could more easily be used from, say, a gradle build script. I’m not a script master, so any thoughts or suggestions would be welcome!

Here is the script, enjoy!

#!/bin/bash
JARFile="build/libs/boot-0.0.1-SNAPSHOT.jar"
PIDFile="app.pid"
PID="-1"
NOPID="-1"

function print_process {
  echo $(<"$PIDFile")
}

function check_pid_file {
  if [ -f $PIDFile ]
  then
    PID=$(print_process)
    return 0;
  else
    PID="-1"
    return 1;
  fi
}

function check_pid_running {
  check_pid_file
  if [ "$PID" == "$NOPID" ]
  then
    return 1    
  fi
  if ps -p $PID > /dev/null
  then
    return 0
  else
    return 1
  fi
}

case "$1" in

status)
  if check_pid_running
  then
    echo "Process is running (" $PID ")"
  else
    echo "Process not running"
  fi
;;

stop)

if check_pid_running
then
  kill -TERM $PID
  echo -ne "Stopping Process"
  NOT_KILLED=1
  for i in {1..30}; do
    if check_pid_running
    then
      echo -ne "."
      sleep 1
    else
      NOT_KILLED=0
    fi
  done
  echo
  if [ $NOT_KILLED = 1 ]
  then
    echo "Cannot kill process " $PID
    exit 1
  fi
  echo "Process stopped"
else
   echo "Process already stopped"
fi
;;

start)
  if check_pid_running
  then
    echo "Process already running"
    exit 1
  fi
  nohup java -jar $JARFile >/dev/null 2>&1 &
  echo "Process started"
;;

debug)
  if check_pid_running
  then
    echo "Process already running"
    exit 0
  fi
  nohup java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=n -jar $JARFile >/dev/null 2>&1 &

  echo "Process started"
;;

restart)
  $0 stop
  if [ $? = 1 ]
  then
    exit 1
  fi
  $0 start
;;

*)
  echo "Usage: $0 {start|stop|restart|debug|status}"
  exit 1

esac

exit 0


Advertisements

Leave a comment

Filed under Software Engineering

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s