Skip to content

Commit e3f65a0

Browse files
chore: initial commit with Spring Boot scripts
1 parent a2a94da commit e3f65a0

File tree

6 files changed

+407
-2
lines changed

6 files changed

+407
-2
lines changed

README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,30 @@
1-
# scripts
2-
some self use tools and scripts
1+
<h1>scripts</h1>
2+
3+
## Description
4+
some self-use tools and scripts
5+
6+
7+
## Available Scripts
8+
9+
| Category | Script Name | Description |
10+
|---------------|---------------|------------------------------------------|
11+
| spring-boot | `start.sh` | Starts the Spring Boot application |
12+
| | `stop.sh` | Stops the Spring Boot application |
13+
| | `restart.sh` | Restart the Spring Boot application cluster |
14+
| | `stopall.sh` | Stops all running Spring Boot instances |
15+
16+
17+
## Installation
18+
No special installation is required. Simply clone the repository to your local machine:
19+
```bash
20+
git clone https://github.com/HaceraI/scripts
21+
cd scripts
22+
```
23+
24+
25+
## Requirements
26+
- **Bash**: The scripts are written in Bash, so they require a Unix-like environment (Linux or macOS). For Windows, you may need to use a Bash emulator like Git Bash.
27+
28+
29+
## License
30+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

Shell/spring-boot/README.md

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# Application Management Scripts
2+
3+
This repository contains a set of shell scripts for managing Java Spring Boot applications. The scripts are designed to help with starting, stopping, and restarting applications, along with logging functionalities.
4+
5+
## Table of Contents
6+
7+
- [Scripts Overview](#scripts-overview)
8+
- [Usage Instructions](#usage-instructions)
9+
- [1. start.sh](#1-startsh)
10+
- [2. stop.sh](#2-stopsh)
11+
- [3. restart.sh](#3-restartsh)
12+
- [4. stopall.sh](#4-stopallsh)
13+
- [Requirements](#requirements)
14+
- [License](#license)
15+
- [Author](#author)
16+
17+
## Scripts Overview
18+
19+
### 1. `start.sh`
20+
21+
This script is used to start a specified Spring Boot application. It checks if the application is already running and handles the initialization of the application jar file.
22+
23+
#### Key Features:
24+
- Automatically finds the jar file if not specified.
25+
- Logs the startup process.
26+
- Supports a restart option to stop and then start the application.
27+
28+
### 2. `stop.sh`
29+
30+
This script stops a running Spring Boot application.
31+
32+
#### Key Features:
33+
- Checks if the application is running before attempting to stop it.
34+
- Logs the stopping process.
35+
- Handles errors during the stopping process.
36+
37+
### 3. `restart.sh`
38+
39+
This script restarts a specified Spring Boot application by first stopping it and then starting it again.
40+
41+
#### Key Features:
42+
- Uses the `stop.sh` script to stop the application.
43+
- Uses the `start.sh` script to start the application.
44+
- Logs the restart process.
45+
46+
### 4. `stopall.sh`
47+
48+
This script stops all specified Spring Boot applications within the subdirectories of the current directory.
49+
50+
#### Key Features:
51+
- Iterates through directories and calls the `stop.sh` script for each application.
52+
- Logs the stopping process for each application.
53+
54+
## Usage Instructions
55+
56+
### 1. start.sh
57+
58+
To start an application:
59+
60+
```bash
61+
./start.sh [options]
62+
```
63+
64+
**Options:**
65+
- `-r`: Restart the application if it is already running.
66+
67+
**Example:**
68+
69+
```bash
70+
./start.sh
71+
```
72+
73+
### 2. stop.sh
74+
75+
To stop an application:
76+
77+
```bash
78+
./stop.sh
79+
```
80+
81+
### 3. restart.sh
82+
83+
To restart an application:
84+
85+
```bash
86+
./restart.sh
87+
```
88+
89+
### 4. stopall.sh
90+
91+
To stop all applications in the subdirectories:
92+
93+
```bash
94+
./stopall.sh [directory1 directory2 ...]
95+
```
96+
97+
If no directories are specified, it will attempt to stop applications in all subdirectories of the current directory.
98+
99+
## Requirements
100+
101+
- Bash shell
102+
- Java Runtime Environment (JRE)
103+
- Proper permissions to execute scripts
104+
105+
## License
106+
107+
This project is licensed under the Apache 2.0 License. See the [LICENSE](LICENSE) file for details.
108+
109+
## Author
110+
111+
- **Haceral** - [[email protected]](mailto:[email protected])
112+
113+
Feel free to contribute to this project or reach out if you have any questions or suggestions!

Shell/spring-boot/restart.sh

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Author: Haceral <[email protected]>
2+
# Version: 1.0.0
3+
# Last update date: 2024/10/12
4+
#!/bin/bash
5+
6+
# Get the root directory of the current script (the starting directory of script execution)
7+
ROOT_DIR=$(dirname "$(readlink -f "$0")")
8+
9+
# Log file, enforced to use the root directory path
10+
LOG_FILE="$ROOT_DIR/restart_logger.log"
11+
12+
# Clear the log file
13+
> "$LOG_FILE"
14+
15+
# Function to restart the application
16+
restart_app() {
17+
local dir=$1
18+
local app_name=$(basename "$dir")
19+
20+
echo "Restarting $app_name ..." | tee -a "$LOG_FILE"
21+
22+
(
23+
# Change to the target application directory
24+
cd "$dir" || { echo "Cannot enter directory $dir, skipping $app_name" | tee -a "$LOG_FILE"; return; }
25+
26+
# Call start.sh and run it in the background
27+
if [ -f "./start.sh" ]; then
28+
# Run start.sh in the background
29+
bash "./start.sh" -r > /dev/null 2>&1 &
30+
start_pid=$!
31+
32+
# Wait for a short period to check if start.sh starts successfully
33+
sleep 5 # Adjust based on application startup time
34+
if ps -p $start_pid > /dev/null; then
35+
echo "$app_name started successfully, PID: $start_pid" | tee -a "$LOG_FILE"
36+
else
37+
echo "$app_name failed to start" | tee -a "$LOG_FILE"
38+
fi
39+
else
40+
echo "Start script does not exist, skipping $app_name" | tee -a "$LOG_FILE"
41+
fi
42+
43+
echo "----------------------------------------" | tee -a "$LOG_FILE"
44+
)
45+
}
46+
47+
# Get the directories that need to be restarted
48+
if [ "$#" -eq 0 ]; then
49+
dirs=(*/)
50+
else
51+
dirs=("$@")
52+
fi
53+
54+
for dir in "${dirs[@]}"; do
55+
# Remove the trailing slash
56+
dir="${dir%/}"
57+
58+
# Check if the directory exists
59+
if [ -d "$dir" ]; then
60+
# Check if start.sh script exists
61+
if [ -f "$dir/start.sh" ]; then
62+
restart_app "$dir"
63+
else
64+
echo "$dir is not a JAR application directory, skipping" | tee -a "$LOG_FILE"
65+
fi
66+
else
67+
echo "$dir directory does not exist, skipping" | tee -a "$LOG_FILE"
68+
fi
69+
done
70+
71+
echo "All applications have been restarted." | tee -a "$LOG_FILE"

Shell/spring-boot/start.sh

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# Author: Haceral <[email protected]>
2+
# Version: 1.0.0
3+
# Last update date: 2024/10/12
4+
#!/bin/bash
5+
6+
# Get the directory where the script is located
7+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
8+
9+
# Define Java startup parameters
10+
JAVA_MEMORY_OPTIONS="-Xms256m -Xmx256m -Xmn128m"
11+
JAVA_VM_OPTIONS="-DNACOS_HOST=localhost -DNACOS_NAMESPACE=60fc076a-b03e-4e17-af31-b22485e734e4 -DNACOS_GROUP=DEFAULT_GROUP"
12+
SPRING_PROFILE=""
13+
JAVA_EXEC="/usr/local/java/jdk8u422/bin/java"
14+
15+
SPRINGBOOT_JAR_NAME=''
16+
17+
# Check for parameters
18+
RESTART_FLAG=false
19+
while getopts ":r" opt; do
20+
case ${opt} in
21+
r )
22+
RESTART_FLAG=true
23+
;;
24+
\? )
25+
echo "Invalid option: -$OPTARG" 1>&2
26+
exit 1
27+
;;
28+
esac
29+
done
30+
31+
# If JAR name is not configured, automatically find and configure it
32+
if [ -z "$SPRINGBOOT_JAR_NAME" ]; then
33+
echo -e "\e[32mDetected that you have not configured the service file name, the system is automatically configuring it....\e[0m"
34+
SPRINGBOOT_JAR_NAME=$(find "$SCRIPT_DIR" -name "*.jar" -print -quit) # Find the first JAR file
35+
if [ -z "$SPRINGBOOT_JAR_NAME" ]; then
36+
echo -e "\e[31mNo JAR file found, exiting...\e[0m"
37+
exit 1
38+
fi
39+
SPRINGBOOT_JAR_NAME=${SPRINGBOOT_JAR_NAME##*/} # Extract the file name
40+
echo -e "\e[32mInitialization complete, your service file name is:\e[31m$SPRINGBOOT_JAR_NAME\e[0m"
41+
sleep 2
42+
fi
43+
44+
# Get the PID of the running service
45+
PIDS=$(pgrep -f "$SPRINGBOOT_JAR_NAME")
46+
47+
# If a restart is detected
48+
if [ "$RESTART_FLAG" = true ]; then
49+
if [ -n "$PIDS" ]; then
50+
echo -e "\e[33mStopping $SPRINGBOOT_JAR_NAME (PID: $PIDS)...\e[0m"
51+
kill -9 $PIDS
52+
sleep 2
53+
echo -e "\e[32m$SPRINGBOOT_JAR_NAME has stopped.\e[0m"
54+
else
55+
echo -e "\e[33mNo running instance of $SPRINGBOOT_JAR_NAME detected, preparing to start...\e[0m"
56+
fi
57+
# Start the service
58+
nohup $JAVA_EXEC -jar $JAVA_MEMORY_OPTIONS $JAVA_VM_OPTIONS "$SCRIPT_DIR/$SPRINGBOOT_JAR_NAME" $SPRING_PROFILE > "$SCRIPT_DIR/run.log" 2>&1 &
59+
sleep 2
60+
echo -e "\e[32m$SPRINGBOOT_JAR_NAME started successfully!\e[0m"
61+
tail -f "$SCRIPT_DIR/run.log"
62+
exit 0
63+
fi
64+
65+
# If a restart is not needed, check if the service is already running
66+
if [ -z "$PIDS" ]; then
67+
nohup $JAVA_EXEC -jar $JAVA_MEMORY_OPTIONS $JAVA_VM_OPTIONS "$SCRIPT_DIR/$SPRINGBOOT_JAR_NAME" $SPRING_PROFILE > "$SCRIPT_DIR/run.log" 2>&1 &
68+
sleep 2
69+
echo -e "\e[32m$SPRINGBOOT_JAR_NAME started successfully!\e[0m"
70+
tail -f "$SCRIPT_DIR/run.log"
71+
exit 0
72+
fi
73+
74+
echo -e "\e[31mWarning: $SPRINGBOOT_JAR_NAME is already running!\e[0m"
75+
exit 1

Shell/spring-boot/stop.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Author: Haceral <[email protected]>
2+
# Version: 1.0.0
3+
# Last update date: 2024/10/12
4+
#!/bin/bash
5+
6+
# Get the directory where the script is located
7+
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
8+
9+
# Initialize JAR name
10+
SPRINGBOOT_JAR_NAME=''
11+
12+
# Automatically find and configure JAR name
13+
if [ -z "$SPRINGBOOT_JAR_NAME" ]; then
14+
echo -e "\e[32mDetected that you have not configured the service file name, the system is automatically configuring it....\e[0m"
15+
SPRINGBOOT_JAR_NAME=$(find "$SCRIPT_DIR" -name "*.jar" -print -quit) # Find the first JAR file
16+
if [ -z "$SPRINGBOOT_JAR_NAME" ]; then
17+
echo -e "\e[31mNo JAR file found, exiting...\e[0m"
18+
exit 1
19+
fi
20+
SPRINGBOOT_JAR_NAME=${SPRINGBOOT_JAR_NAME##*/} # Extract the file name
21+
echo -e "\e[32mInitialization complete, your service file name is:\e[31m$SPRINGBOOT_JAR_NAME\e[0m"
22+
fi
23+
24+
# Get the PID of the running service
25+
PIDS=$(pgrep -f "$SPRINGBOOT_JAR_NAME")
26+
27+
# Check if the service is running
28+
if [ -z "$PIDS" ]; then
29+
echo -e "\e[31mWarning: $SPRINGBOOT_JAR_NAME is not running!\e[0m"
30+
exit 1
31+
fi
32+
33+
# Stop the service
34+
echo -e "Stopping $SPRINGBOOT_JAR_NAME process [$PIDS] ...\c"
35+
for PID in $PIDS; do
36+
kill "$PID" > /dev/null 2>&1
37+
if [ $? -ne 0 ]; then
38+
echo -e "\e[31mFailed to stop process $PID!\e[0m"
39+
fi
40+
done
41+
42+
# Wait for the process to stop
43+
while true; do
44+
sleep 2
45+
if ! ps -p $PIDS > /dev/null; then
46+
break
47+
fi
48+
echo -e "......\c"
49+
done
50+
51+
echo "$SPRINGBOOT_JAR_NAME process [$PIDS] has been stopped!"

0 commit comments

Comments
 (0)