SoFunction
Updated on 2025-05-20

Scripting issues for deploying springBoot projects in Linux

Scripts for deploying springBoot projects in Linux

Restart

#!/bin/bash

# Define variablesJAR_NAME=""
LOG_DIR="logs/xxx"

# Create a log directorymkdir -p "$LOG_DIR"

# Find the process ID that occupies the portPID=$(ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}')

echo "PID: ${PID:-not found}"

echo "Restarting service..."

# Stop the processif [ -n "$PID" ]; then
    echo "Stopping existing process (PID: $PID)..."
    kill -9 "$PID" > /dev/null 2>&1
fi

# Wait for 2 secondssleep 2

# Start a new processecho "Starting application..."
nohup java -jar "$JAR_NAME" >> "$LOG_DIR/" 2>&1 &

# Check whether the startup is successfulif [ $? -eq 0 ]; then
    echo "Application started successfully"
    echo "PID: $!"
else
    echo "Failed to start application"
    exit 1
fi

# Keep the terminal from exitingread -p "Press any key to continue..." -n1 -s

stop

#!/bin/bash

JAR_NAME=""

# Get the process PID that listens to the portPID=$(ps --no-heading -C java -f --width 1000 | grep $JAR_NAME | awk '{print $2}')

echo "PID: ${PID:-not found}"

if [ -z "$PID" ]; then
    echo "PID not found. Server may not be running."
else
    echo "Stopping existing process (PID: $PID)..."
    kill -9 "$PID" > /dev/null 2>&1
    # Check whether it is successful    if [ $? -eq 0 ]; then
        echo "Stop Success!"
    else
        echo "Failed to stop the server."
    fi
fi

read -p "Press any key to continue..." -n1 -s

illustrate:

When maven is packaged, you can package the script and jar in the same directory as the jar

   <fileSet>
     <directory>bin/linux</directory>
     <includes>
       <include></include>
       <include></include>
     </includes>
     <outputDirectory>/</outputDirectory>
   </fileSet>

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.