sh update

This commit is contained in:
577732344@qq.com
2025-09-24 11:22:01 +08:00
parent 230c430284
commit 85d6b57314

View File

@@ -32,37 +32,77 @@ __red() {
} }
__kill() { __kill() {
local process_name=$1 local jar_path="$1"
PID=$(ps -ef | grep "${process_name}.jar" | grep -v grep | awk '{print $2}' | xargs)
local sleep_seconds=10 local sleep_seconds=10
local cur_sleep_second=1 local cur_sleep_second=1
local pids=()
if [[ -z "$PID" ]]; then # 使用临时文件或直接遍历 jps 输出(避免进程替换)
__green "进程 ${process_name} 未运行" jps -l 2>/dev/null | while read -r pid main_class; do
if [ "$main_class" = "$jar_path" ]; then
echo "$pid"
fi
done > /tmp/ruoyi_pids.$$
# 读取匹配的 PID 到数组(在当前 shell 中)
while IFS= read -r pid; do
[ -n "$pid" ] && pids+=("$pid")
done < /tmp/ruoyi_pids.$$
# 清理临时文件
rm -f /tmp/ruoyi_pids.$$
if [ ${#pids[@]} -eq 0 ]; then
__green "未找到运行中的进程: $jar_path"
return 0 return 0
fi fi
while [[ -n "$PID" && "$sleep_seconds" -ge "$cur_sleep_second" ]]; do __green "找到 ${#pids[@]} 个匹配进程: ${pids[*]}"
__green "尝试kill PID: ${PID}"
if [ "$sleep_seconds" -eq "$cur_sleep_second" ]; then # 发送 SIGTERM
__red "强制关闭: ${cur_sleep_second}" for pid in "${pids[@]}"; do
kill -9 $PID >> /dev/null 2>&1 __green "发送 SIGTERM 到 PID: $pid"
else kill "$pid" >/dev/null 2>&1
kill $PID >> /dev/null 2>&1
fi
__green "停止程序计时: ${cur_sleep_second}"
sleep 1
cur_sleep_second="$(_math "$cur_sleep_second" + 1)"
PID=$(ps -ef | grep "${process_name}.jar" | grep -v grep | awk '{print $2}' | xargs)
done done
if [[ -n "$PID" ]]; then # 等待退出
__red "无法停止进程 ${process_name}, PID: ${PID}" while [ $cur_sleep_second -le $sleep_seconds ]; do
remaining=()
for pid in "${pids[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
remaining+=("$pid")
fi
done
if [ ${#remaining[@]} -eq 0 ]; then
__green "所有进程已成功停止"
return 0
fi
__green "等待进程退出... (${cur_sleep_second}/${sleep_seconds} 秒)"
sleep 1
cur_sleep_second=$((cur_sleep_second + 1))
done
# 强制 kill -9
__red "优雅关闭超时,强制终止剩余进程: ${remaining[*]}"
for pid in "${remaining[@]}"; do
kill -9 "$pid" 2>/dev/null
done
# 检查是否还有存活
still_alive=()
for pid in "${remaining[@]}"; do
if kill -0 "$pid" 2>/dev/null; then
still_alive+=("$pid")
fi
done
if [ ${#still_alive[@]} -gt 0 ]; then
__red "无法终止进程: ${still_alive[*]}"
return 1 return 1
else else
__green "进程 ${process_name} 已成功停止" __green "所有进程已强制终止"
return 0 return 0
fi fi
} }