Jar/War 구동 쉘 스크립트

  • Post author:
  • Post category:Linux

SpringBoot Jar/War 파일을 리눅스에서 백그라운드로 구동시 필요한 쉘스크립트를 작성하였습니다.
SERVICE_NAME : 서비스 이름을 설정합니다.
SERVICE_PATH : 서비스 경로를 설정합니다.
JAR_NAME : 구동하려는 Jar/War 파일명을 설정합니다.
구동은 run.sh {start | stop | restart | status } 로 구동합니다.

#!/bin/bash

#--------------------------------------------------------
# service information
#--------------------------------------------------------
SERVICE_NAME="WAS ADMIN SERVER"
SERVICE_PATH=/sw/was_admin
JAR_NAME=was_admin-0.0.1-SNAPSHOT.war
PID_NAME=was.pid

#--------------------------------------------------------
# java run information
#--------------------------------------------------------a
JAVA_PATH="java"
#JAVA_OPT="-Xms1024m -Xmx1024m"
JAVA_OPT=""
RUN_SUDO="sudo"
RUN_NOHUP="nohup"

start()
{
   echo "------------------------------------------------------"
   echo "   Starting $SERVICE_NAME ..."
   echo "------------------------------------------------------"

   local CHECK_PID=$(get_status)

   if [ -n "${CHECK_PID}" ]; then
      echo -e "${SERVICE_NAME} is already running...\n"
      ps -ef | grep ${JAR_NAME} | grep -v grep
      echo ""
      exit 0
   fi
   rm -f $SERVICE_PATH/$PID_NAME
   $RUN_SUDO $RUN_NOHUP $JAVA_PATH -jar $JAVA_OPT $SERVICE_PATH/$JAR_NAME >> /dev/null 2>&1 &
   local PID=$!
   echo $PID > $SERVICE_PATH/$PID_NAME

   sleep 1

   if [ -n ${PID_NAME} ]; then
      echo "Starting..."

      local CHECK_PID=$(get_status)
      if [ -n "${CHECK_PID}" ]; then
         echo -e "Created Process ID : $PID"
         echo -e "Process started...\n"
         #ps -ef | grep ${JAR_NAME} | grep -v grep
         #echo ""
         exit 0
      else
         echo -e "Failed to start...\n"
      fi
   else
      echo -e "Failed to start...\n"
   fi

}

stop()
{
   echo "------------------------------------------------------"
   echo "   Stopping $SERVICE_NAME ..."
   echo "------------------------------------------------------"

   local CHECK_PID=$(get_status)
   if [ -n "${CHECK_PID}" ]; then
      ps -ef | grep "$JAR_NAME" | grep -v grep | awk '{print $2}' | xargs $RUN_SUDO kill -9 2>/dev/null
      if [ $? -eq 0 ];then
         rm -f $SERVICE_PATH/$PID_NAME

         echo -e "Stop success...\n"
         exit 0
      fi
   else
      rm -f $SERVICE_PATH/$PID_NAME

      echo -e "Process is not running...\n"
   fi
}

restart()
{
   echo "------------------------------------------------------"
   echo "   Restaring $SERVICE_NAME ..."
   echo "------------------------------------------------------"

   # stop process
   echo "Process stopping..."
   ps -ef | grep "$JAR_NAME" | grep -v grep | awk '{print $2}' | xargs $RUN_SUDO kill -9 2>/dev/null
   echo -e "Process stopped...\n"
   rm -f $SERVICE_PATH/$PID_NAME

   # start process
   rm -f $SERVICE_PATH/$PID_NAME
   $RUN_SUDO $RUN_NOHUP $JAVA_PATH -jar $JAVA_OPT $SERVICE_PATH/$JAR_NAME >> /dev/null 2>&1 &
   local PID=$!
   echo $PID > $SERVICE_PATH/$PID_NAME

   sleep 1

   if [ -n ${PID_NAME} ]; then
      echo "Starting..."

      local CHECK_PID=$(get_status)
      if [ -n "${CHECK_PID}" ]; then
         echo -e "Created Process ID : $PID"
         echo -e "Process started...\n"
         #ps -ef | grep ${JAR_NAME} | grep -v grep
         #echo ""
         exit 0
      else
         echo -e "Failed to start...\n"
      fi
   else
      echo -e "Failed to start...\n"
   fi
}

status()
{
   echo "------------------------------------------------------"
   echo "   Status $SERVICE_NAME ..."
   echo "------------------------------------------------------"

   local CHECK_PID=$(get_status)
   if [ -n "${CHECK_PID}" ]; then
      echo -e "Process is running...\n"
      ps -ef | grep ${JAR_NAME} | grep -v grep
      echo ""
   else
      echo -e "Process is not running...\n"
   fi
}

get_status()
{
   ps aux | grep ${JAR_NAME} | grep -v grep | awk '{print $2}'
}

case $1 in
    start)
      start
    ;;

    stop)
      stop
    ;;

    restart)
      restart
    ;;

    status)
      status
    ;;

    *)
      echo "------------------------------------------------------"
      echo "   $SERVICE_NAME ..."
      echo "------------------------------------------------------"
      echo -e "Run with parameter (start, stop, restart, status)\n"
    ;;
esac