How to start and stop SpringBoot jar program deployment shell script in Linux

How to start and stop SpringBoot jar program deployment shell script in Linux

Without further ado, let me give you the code. The specific code is as follows:

#!/bin/bash
cd `dirname $0`
CUR_SHELL_DIR=`pwd`
CUR_SHELL_NAME=`basename ${BASH_SOURCE}`
#Modify the jar package name here JAR_NAME="xxxxxxxxxxxx.jar" 
JAR_PATH=$CUR_SHELL_DIR/$JAR_NAME
#JAVA_MEM_OPTS=" -server -Xms1024m -Xmx1024m -XX:PermSize=128m"
JAVA_MEM_OPTS=""
#SPRING_PROFILES_ACTIV="-Dspring.profiles.active=eureka2"
SPRING_PROFILES_ACTIV=""
LOG_DIR=$CUR_SHELL_DIR/logs
LOG_PATH=$LOG_DIR/${JAR_NAME}.log
echo_help()
{
  echo -e "syntax: sh $CUR_SHELL_NAME start|stop"
}
if [ -z $1 ];then
  echo_help
  exit 1
fi
if [ ! -d "$LOG_DIR" ];then
  mkdir "$LOG_DIR"
fi
if [ ! -f "$LOG_PATH" ];then
  touch "$LOG_DIR"
fi
if [ "$1" == "start" ]; then
# check server
  PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
  if [ -n "$PIDS" ]; then
    echo -e "ERROR: The $JAR_NAME already started and the PID is ${PIDS}."
    exit 1
  fi
echo "Starting the $JAR_NAME..."
 
  # start
  nohup java $JAVA_MEM_OPTS -jar $SPRING_PROFILES_ACTIV $JAR_PATH >> $LOG_PATH 2>&1 &
 
  COUNT=0
  while [ $COUNT -lt 1 ]; do
    sleep 1
    COUNT=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}' | wc -l`
    if [ $COUNT -gt 0 ]; then
      break
    fi
  done
  PIDS=`ps --no-heading -C java -f --width 1000 | grep "$JAR_NAME" | awk '{print $2}'`
  echo "${JAR_NAME} Started and the PID is ${PIDS}."
  echo "You can check the log file in ${LOG_PATH} for details."
 
elif [ "$1" == "stop" ];then
 
  PIDS=`ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}'`
  if [ -z "$PIDS" ]; then
    echo "ERROR:The $JAR_NAME does not started!"
    exit 1
  fi
echo -e "Stopping the $JAR_NAME..."
 
  for PID in $PIDS; do
    kill $PID > /dev/null 2>&1
  done
 
  COUNT=0
  while [ $COUNT -lt 1 ]; do
    sleep 1
    COUNT=1
    for PID in $PIDS ; do
      PID_EXIST=`ps --no-heading -p $PID`
      if [ -n "$PID_EXIST" ]; then
        COUNT=0
        break
      fi
    done
  done
 
  echo -e "${JAR_NAME} Stopped and the PID is ${PIDS}."
else
  echo_help
  exit 1
fi

The above code can be used by simply changing the jar package name

Start the jar package:

./xxxx.sh start

Stop jar package

./xxxx.sh stop

Summarize

The above is the method of starting and stopping the SpringBoot jar program in Linux to deploy the Shell script. I hope it will be helpful to everyone. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Methods and steps to deploy springboot project under Linux
  • How to package and deploy springboot to linux server
  • Detailed tutorial on deploying SpringBoot + Vue project to Linux server
  • How to deploy Spring boot to Linux
  • Deploy Spring Boot program using Linux
  • How to deploy springboot on linux to access server resources
  • Let your Spring Boot project run on Linux server from scratch

<<:  Vue realizes price calendar effect

>>:  A brief discussion on Mysql specified order sorting query

Recommend

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

How to declare a cursor in mysql

How to declare a cursor in mysql: 1. Declare vari...

Detailed explanation of nginx proxy_cache cache configuration

Preface: Due to my work, I am involved in the fie...

Detailed explanation of CSS3 rotating cube problem

3D coordinate concept When an element rotates, it...

How to use CocosCreator to create a shooting game

Analyze the production steps: 1. Prepare resource...

Small paging design

Let our users choose whether to move forward or ba...

Selection and thinking of MySQL data backup method

Table of contents 1. rsync, cp copy files 2. sele...

Full steps to create a high-performance index in MySQL

Table of contents 1. Index Basics 1. Types of Ind...

Create a code example of zabbix monitoring system based on Dockerfile

Use the for loop to import the zabbix image into ...

MySQL 5.7.17 installation and configuration graphic tutorial

The blogger said : I have been writing a series o...

How to upload and download files between Linux server and Windows system

Background: Linux server file upload and download...

Vue uses Echarts to implement a three-dimensional bar chart

This article shares the specific code of Vue usin...

Detailed tutorial on Tomcat installation and deployment in Windows 10

Table of contents 1 Java environment configuratio...

Detailed explanation of MySQL binlog usage

binlog is a binary log file that records all DML ...

Introduction to the usage of common XHTML tags

There are many tags in XHTML, but only a few are ...