-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdevelop.sh
More file actions
executable file
·67 lines (60 loc) · 1.56 KB
/
Copy pathdevelop.sh
File metadata and controls
executable file
·67 lines (60 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
#
CLEANING=0
cleanup() {
[ $CLEANING -eq 1 ] && return
CLEANING=1
echo "正在停止所有子进程..."
# 先发 SIGTERM 给子进程
[ -n "$GO_PID" ] && kill $GO_PID 2>/dev/null
[ -n "$NPM_PID" ] && kill $NPM_PID 2>/dev/null
# 尝试等待子进程优雅退出
sleep 1
# 强制杀掉仍存活的子进程
[ -n "$GO_PID" ] && kill -9 $GO_PID 2>/dev/null
[ -n "$NPM_PID" ] && kill -9 $NPM_PID 2>/dev/null
wait 2>/dev/null
}
trap cleanup EXIT INT TERM
# 释放占用的端口
kill_port() {
local port=$1
if command -v lsof &>/dev/null; then
local pids
pids=$(lsof -ti tcp:"$port" 2>/dev/null)
[ -n "$pids" ] && kill $pids 2>/dev/null
elif command -v fuser &>/dev/null; then
fuser -k "${port}/tcp" 2>/dev/null
fi
}
kill_port 8080
kill_port 3000
# 启动 Go 服务
if [ ! -f .local.yml ]; then
cp config.yml .local.yml
fi
CONFIG_PATH=.local.yml go run cmd/server/main.go &
GO_PID=$!
# 启动 NPM 服务
cd webview
npm i
npm run dev &
NPM_PID=$!
# 等待任意一个子进程退出(兼容 macOS 默认 bash 3.2,不支持 wait -n)
EXIT_CODE=0
while kill -0 $GO_PID 2>/dev/null && kill -0 $NPM_PID 2>/dev/null; do
sleep 1
done
if ! kill -0 $GO_PID 2>/dev/null; then
wait $GO_PID 2>/dev/null
EXIT_CODE=$?
else
wait $NPM_PID 2>/dev/null
EXIT_CODE=$?
fi
if [ $EXIT_CODE -gt 128 ]; then
echo "收到终止信号,正在停止所有进程..."
else
echo "检测到子进程退出(退出码: $EXIT_CODE),正在终止其他进程..."
fi
exit $EXIT_CODE