Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
custom-server
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lizhonghong
custom-server
Commits
07b66491
Commit
07b66491
authored
Jun 25, 2026
by
Lizh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改优化启动和停止脚本
parent
8a419ff9
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
74 additions
and
27 deletions
+74
-27
custom-server-starter/src/main/bin/startup.sh
+44
-15
custom-server-starter/src/main/bin/stop.sh
+30
-12
No files found.
custom-server-starter/src/main/bin/startup.sh
View file @
07b66491
...
...
@@ -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
=
2
0
local
health_url
=
"http://localhost:
${
SVR_PORT
}
/api/v3/actuator/health"
local
poll_interval
=
1
0
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
custom-server-starter/src/main/bin/stop.sh
View file @
07b66491
...
...
@@ -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
echo
"
$APP_NAME
is not running."
exit
0
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."
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment