Linux editing start, stop and restart springboot jar package script example

Linux editing start, stop and restart springboot jar package script example

Preface

In the springboot configuration file, the names of the configuration files have their own meanings and uses

  • dev Development Environment
  • prod production environment (default)
  • test test environment

Load the specified profile --spring.profiles.active=prod

Springboot loads jar packages in the following ways:

// Start directly in the console. The disadvantage is that the project will be closed when the console is closed.
java -jar bootdo.jar
// This method can run in the background, but if the shell is launched, it will also hang java -jar /bootdo-2.0.0.jar > bootdolog.file 2>&1 &
// If nohup is added, it will not be affected even if the shell is exited.
nohup java -jar /bootdo-2.0.0.jar > bootdolog.file 2>&1 &

explain

nohup means running permanently. & indicates background operation

> represents where to redirect to

1 means stdout standard output, the system default value is 1, so ">/dev/null" is equivalent to "1>/dev/null"

2 means stderr standard error

nohup ./mqnamesrv >/home/cxb/mqnamesrv.out 2>&1 & That is, the standard output is sent to mqnamesrv.out. Then, the standard error output is redirected to the same file as the standard output.

After the server is successfully started in the following way, if it involves a restart, you need to query the process ID through ps -ef | grep bootdo , then kill it through kill -s 9 ${pid} and restart it, which is very troublesome.

nohup java -jar /bootdo-2.0.0.jar > bootdolog.file 2>&1 & 

It's okay if it happens once or twice, but if it happens multiple times, it will be a bit overwhelming.

In this way, you can write a shell script to start (start), stop (stop), and restart (restart) operations in one step, which is convenient and efficient

Create a wss.sh script in a custom directory and edit the content as follows.

#!/bin/bash
#This can be replaced with your own executable program. No other code needs to be changed APP_NAME=websocketserver-0.0.1-SNAPSHOT.jar
 
#Instructions for use, used to prompt for parameter input usage() {
 echo "Usage: sh script name.sh [start|stop|restart|status]"
 exit 1
}
 
# Check if the program is running is_exist(){
 pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
 # If it does not exist, return 1; if it does exist, return 0 
 if [ -z "${pid}" ]; then
 return 1
 else
 return 0
 fi
}
 
#Start method start(){
 is_exist
 if [ $? -eq "0" ]; then
 echo "${APP_NAME} is already running. pid=${pid} ."
 else
 nohup java -jar /mnt/ssd1/project/websocket/$APP_NAME > /mnt/ssd1/project/websocket/websocketserverlog.file 2>&1 &
 echo "${APP_NAME} start success"
 fi
}
 
#Stop method stop(){
 is_exist
 if [ $? -eq "0" ]; then
 kill -9 $pid
 else
 echo "${APP_NAME} is not running"
 fi 
}
 
# Output running status status(){
 is_exist
 if [ $? -eq "0" ]; then
 echo "${APP_NAME} is running. Pid is ${pid}"
 else
 echo "${APP_NAME} is NOT running."
 fi
}
 
#Restart restart(){
 stop
 start
}
 
#According to the input parameters, select the corresponding method to execute. If no input is given, the method will be executed. Instructions for use case "$1" in
 "start")
 start
 ;;
 "stop")
 stop
 ;;
 "status")
 status
 ;;
 "restart")
 restart
 ;;
 *)
 usage
 ;;
esac 

Configure the startup command in the row marked in red.

After that, you can use wss.sh start | stop | restart to start, stop, and restart.

Add

Difference between sh xxx.sh and ./xxx.sh

sh xxx.sh does not need to have execution permission

./xxx.sh needs to have execution permission, which can be granted via chmod +x xxx.sh

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • Using springboot jar package can be started as service
  • Using springboot jar package can be started as service
  • A detailed tutorial on how to build, package, and start jar and war packages in SpringBoot
  • SpringBoot project running jar package startup step process analysis
  • How to start the SpringBoot jar package

<<:  Vue implements irregular screenshots

>>:  Solve the group by query problem after upgrading Mysql to 5.7

Recommend

MySQL whole table encryption solution keyring_file detailed explanation

illustrate MySql Community Edition supports table...

Learn Node.js from scratch

Table of contents url module 1.parse method 2. fo...

JavaScript to implement mobile signature function

This article shares the specific code of JavaScri...

Detailed explanation of real-time backup knowledge points of MySQL database

Preface The need for real-time database backup is...

Solutions to browser interpretation differences in size and width and height in CSS

Let’s look at an example first Copy code The code ...

An article to help you understand Js inheritance and prototype chain

Table of contents Inheritance and prototype chain...

A brief discussion on three methods of asynchronous replication in MySQL 8.0

In this experiment, we configure MySQL standard a...

CSS horizontal centering and limiting the maximum width

A CSS layout and style question: how to balance h...

MySQL 5.7.18 version free installation configuration tutorial

MySQL is divided into installation version and fr...

Introduction and examples of hidden fields in HTML

Basic syntax: <input type="hidden" na...

A quick guide to Docker

Docker provides a way to automatically deploy sof...

CSS Paint API: A CSS-like Drawing Board

1. Use Canvas images as CSS background images The...

...