Commit 07b66491 by Lizh

修改优化启动和停止脚本

parent 8a419ff9
......@@ -2,29 +2,55 @@
APP_NAME="custom-server"
APP_JAR="custom-server-starter-1.0.jar"
LOG_FILE="logs/custom-server.log"
JAVA_HOME="/home/lizh/jdk-17.0.19"
SVR_PORT=40071
PID_FILE="$APP_NAME.pid"
# 检查进程是否存在
PID=$(ps -ef | grep $APP_JAR | grep -v grep | awk '{print $2}')
if [ -n "$PID" ]; then
echo "Stopping $APP_NAME (PID: $PID)..."
kill -TERM $PID
sleep 5
if [ -f "$PID_FILE" ]; then
OLD_PID=$(cat "$PID_FILE")
if kill -0 "$OLD_PID" 2>/dev/null; then
if "$JAVA_HOME/bin/jps" -l 2>/dev/null | grep -q "^${OLD_PID} .*${APP_JAR}"; then
echo "$APP_NAME is already running (PID: $OLD_PID)."
exit 1
fi
fi
rm -f "$PID_FILE"
fi
echo "Starting $APP_NAME..."
nohup $JAVA_HOME/bin/java -Xms2g -Xmx4g -XX:+UseG1GC -jar $APP_JAR --spring.profiles.active=dev --spring.config.location=file:config/ >> $LOG_FILE 2>&1 &
nohup $JAVA_HOME/bin/java -Xms2g -Xmx4g -XX:+UseG1GC \
-jar $APP_JAR \
--spring.profiles.active=prod \
--spring.config.location=file:config/ \
>> /dev/null 2>&1 &
if [ $? -ne 0 ]; then
NEW_PID=$!
if [ -z "$NEW_PID" ]; then
echo "$APP_NAME start failed: process launch error."
exit 1
fi
if ! kill -0 "$NEW_PID" 2>/dev/null; then
echo "$APP_NAME start failed: process exited immediately."
exit 1
fi
echo "$NEW_PID" > "$PID_FILE"
echo "$APP_NAME started (PID: $NEW_PID)."
cleanup() {
echo "Cleaning up $APP_NAME (PID: $NEW_PID)..."
kill -TERM "$NEW_PID" 2>/dev/null
sleep 2
kill -KILL "$NEW_PID" 2>/dev/null
rm -f "$PID_FILE"
}
trap cleanup EXIT
serverCheck() {
local health_url="http://localhost:40071/api/v3/actuator/health"
local poll_interval=20
local health_url="http://localhost:${SVR_PORT}/api/v3/actuator/health"
local poll_interval=10
local max_wait=60
local elapsed=0
......@@ -34,7 +60,6 @@ serverCheck() {
sleep $poll_interval
elapsed=$((elapsed + poll_interval))
# 一次 curl 同时获取 HTTP 状态码和响应体,避免二次请求
local response
response=$(curl -s -w "\n%{http_code}" "$health_url" 2>/dev/null)
local http_code=$(echo "$response" | tail -1)
......@@ -45,11 +70,15 @@ serverCheck() {
return 0
fi
printf " ... %2ds / %2ds (HTTP %s)\n" $elapsed $max_wait "${http_code:-N/A}"
printf "waiting ... %2ds / %2ds (HTTP %s)\n" $elapsed $max_wait "${http_code:-N/A}"
done
echo "$APP_NAME start failed: health check timeout after ${max_wait}s."
return 1
}
serverCheck || exit 1
if serverCheck; then
trap - EXIT
else
exit 1
fi
......@@ -2,17 +2,37 @@
APP_NAME="custom-server"
APP_JAR="custom-server-starter-1.0.jar"
JAVA_HOME="/home/lizh/jdk-17.0.19"
PID_FILE="$APP_NAME.pid"
MAX_WAIT=5
if [ -f "$PID_FILE" ]; then
PID=$(cat "$PID_FILE")
if ! "$JAVA_HOME/bin/jps" -l 2>/dev/null | grep -q "^${PID} .*${APP_JAR}"; then
rm -f "$PID_FILE"
PID=""
fi
else
PID=""
fi
# 检查进程是否存在
PID=$(ps -ef | grep $APP_JAR | grep -v grep | awk '{print $2}')
if [ -z "$PID" ]; then
PID=$("$JAVA_HOME/bin/jps" -l 2>/dev/null | grep " ${APP_JAR}$" | awk '{print $1}')
if [ -z "$PID" ]; then
echo "$APP_NAME is not running."
exit 0
fi
PID_COUNT=$(echo "$PID" | wc -l)
if [ "$PID_COUNT" -gt 1 ]; then
echo "Multiple $APP_NAME instances found (PIDs: $(echo "$PID" | tr '\n' ' '))— please stop manually."
exit 1
fi
echo "(PID file missing, located via jps: $PID)"
fi
# 检查是否为当前用户所启动的进程
PROCESS_USER=$(ps -o user= -p $PID 2>/dev/null)
PROCESS_USER=$(ps -o user= -p "$PID" 2>/dev/null)
CURRENT_USER=$(whoami)
if [ "$PROCESS_USER" != "$CURRENT_USER" ]; then
echo "Cannot stop $APP_NAME (PID: $PID) — owned by $PROCESS_USER, current user is $CURRENT_USER."
......@@ -21,19 +41,16 @@ fi
echo "Stopping $APP_NAME (PID: $PID)..."
# 优雅停止(发送 SIGTERM 信号)
kill -TERM $PID 2>/dev/null || {
kill -TERM "$PID" 2>/dev/null || {
echo "Failed to send SIGTERM to PID $PID."
exit 1
}
# 等待进程退出(最多等待 30 秒)
MAX_WAIT=30
WAIT_COUNT=0
while kill -0 $PID 2>/dev/null; do
while kill -0 "$PID" 2>/dev/null; do
if [ $WAIT_COUNT -ge $MAX_WAIT ]; then
echo "Force killing $APP_NAME (PID: $PID)..."
kill -KILL $PID 2>/dev/null || {
kill -KILL "$PID" 2>/dev/null || {
echo "Failed to force kill PID $PID."
exit 1
}
......@@ -43,4 +60,5 @@ while kill -0 $PID 2>/dev/null; do
WAIT_COUNT=$((WAIT_COUNT + 1))
done
rm -f "$PID_FILE"
echo "$APP_NAME stopped successfully."
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment