Example script for Linux to start and stop the spring boot project

  • 2021-07-03 01:15:59
  • OfStack

There are three ways to start an springboot project:

1. Run the main method program

2. Run on the command line using the command mvn spring-boot: run

3. After packing the bit jar file with mvn packpage, run it using the command line java-jar yourapp. jar

1 Generally, we often use the first two running modes when developing and the third mode when deploying and implementing. Below, we will focus on downloading the instructions and scripts for running and stopping springboot projects in linux environment:

1. Directives

nohup do not hang up operation instruction

1 can be used directly in linux:


java -jar yourapp.jar

To start the program, but once the process 1 terminates, the program will hang up immediately, so in this case, we have to use nohup


nohup java -jar yourapp.jar > yourapp.out 2>&1 &

But the biggest problem is that it is inconvenient to manage. What to do?

2. Write shell scripts

1. start. sh


#!/bin/bash
nohup java -jar yourapp.jar -Xms256m -Xmx1024m > yourapp.out 2>&1 &

2. stop. sh


#!/bin/bash
PID=$(ps -ef | grep yourapp.jar | grep -v grep | awk '{ print $2 }')
if [ ${PID} ]; 
then
 echo 'Application is stpping...'
 echo kill $PID DONE
 kill $PID
else
 echo 'Application is already stopped...'
fi

3. Integrate start and stop scripts and write run. sh


#!/bin/bash
echo 'Application is stpping...'
source stop.sh
echo 'Application is running...'
source start.sh

binggo …

Summarize


Related articles: