-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop.sh
More file actions
executable file
·227 lines (184 loc) · 4.87 KB
/
stop.sh
File metadata and controls
executable file
·227 lines (184 loc) · 4.87 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env bash
set -euo pipefail
echo "=========================================="
echo " Stopping Mini-OpenClaw System"
echo "=========================================="
# Colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
print_status() {
echo -e "${GREEN}[STATUS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PIDS_FILE="${ROOT_DIR}/.pids"
BACKEND_PORT="${BACKEND_PORT:-8002}"
FRONTEND_PORT="${FRONTEND_PORT:-3000}"
# Non-interactive mode:
# - STOP_CLEAN_LOGS=1 => clean logs
# - STOP_CLEAN_LOGS=0 => keep logs
# - --yes / -y => clean logs without prompt
# - --no-clean-logs => keep logs without prompt
ASSUME_YES=0
SKIP_LOG_PROMPT=0
CLEAN_LOGS_FLAG="${STOP_CLEAN_LOGS:-}"
while [[ $# -gt 0 ]]; do
case "$1" in
-y|--yes)
ASSUME_YES=1
SKIP_LOG_PROMPT=1
CLEAN_LOGS_FLAG=1
shift
;;
--no-clean-logs)
SKIP_LOG_PROMPT=1
CLEAN_LOGS_FLAG=0
shift
;;
--clean-logs)
SKIP_LOG_PROMPT=1
CLEAN_LOGS_FLAG=1
shift
;;
*)
print_warning "Unknown argument ignored: $1"
shift
;;
esac
done
is_running_pid() {
local pid="$1"
[[ -n "${pid}" ]] && kill -0 "${pid}" 2>/dev/null
}
terminate_pid() {
local pid="$1"
local name="${2:-process}"
if ! is_running_pid "${pid}"; then
return 0
fi
print_status "Stopping ${name} (PID: ${pid})..."
kill "${pid}" 2>/dev/null || true
for _ in 1 2 3 4 5; do
if ! is_running_pid "${pid}"; then
return 0
fi
sleep 0.4
done
print_warning "${name} (PID: ${pid}) did not exit after SIGTERM, sending SIGKILL..."
kill -9 "${pid}" 2>/dev/null || true
}
kill_by_pattern() {
local pattern="$1"
local name="$2"
# pgrep may return non-zero when nothing matches; that's fine.
local pids
pids="$(pgrep -f "${pattern}" || true)"
if [[ -z "${pids}" ]]; then
return 0
fi
print_status "Found ${name} process(es) by pattern: ${pattern}"
for pid in ${pids}; do
terminate_pid "${pid}" "${name}"
done
}
kill_by_port() {
local port="$1"
local name="$2"
# lsof output may include duplicate PIDs, dedupe with sort -u
local pids
pids="$(lsof -ti TCP:${port} -sTCP:LISTEN 2>/dev/null | sort -u || true)"
if [[ -z "${pids}" ]]; then
return 0
fi
print_status "Found ${name} listener(s) on port ${port}"
for pid in ${pids}; do
terminate_pid "${pid}" "${name}"
done
}
stop_from_pid_file() {
if [[ ! -f "${PIDS_FILE}" ]]; then
print_status "No PID file found."
return 0
fi
print_status "Using PID file: ${PIDS_FILE}"
# shellcheck disable=SC1090
source "${PIDS_FILE}" || true
if [[ -n "${BACKEND_PID:-}" ]]; then
terminate_pid "${BACKEND_PID}" "backend"
fi
if [[ -n "${FRONTEND_PID:-}" ]]; then
terminate_pid "${FRONTEND_PID}" "frontend"
fi
rm -f "${PIDS_FILE}"
print_status "PID file removed."
}
stop_services() {
print_status "Stopping services..."
# 1) Best-effort: stop what we started
stop_from_pid_file
# 2) Pattern-based fallback (covers uv/uvicorn and next dev)
kill_by_pattern "uv run uvicorn backend.app:app" "backend"
kill_by_pattern "uvicorn.*backend.app:app" "backend"
kill_by_pattern "python.*uvicorn.*backend.app:app" "backend"
kill_by_pattern "next dev" "frontend"
kill_by_pattern "node.*next.*dev" "frontend"
# 3) Port-based fallback
kill_by_port "${BACKEND_PORT}" "backend"
kill_by_port "${FRONTEND_PORT}" "frontend"
print_status "Stop routine completed."
}
show_status() {
print_status "Checking service status..."
if curl -fsS "http://localhost:${BACKEND_PORT}/api/health" >/dev/null 2>&1; then
echo "Backend: ❌ Still running on port ${BACKEND_PORT}"
else
echo "Backend: ✅ Stopped"
fi
if curl -fsS "http://localhost:${FRONTEND_PORT}" >/dev/null 2>&1; then
echo "Frontend: ❌ Still running on port ${FRONTEND_PORT}"
else
echo "Frontend: ✅ Stopped"
fi
}
should_clean_logs() {
if [[ "${SKIP_LOG_PROMPT}" -eq 1 ]]; then
[[ "${CLEAN_LOGS_FLAG:-0}" == "1" ]]
return
fi
if [[ "${ASSUME_YES}" -eq 1 ]]; then
return 0
fi
if [[ ! -t 0 ]]; then
# Non-interactive stdin defaults to "no clean" unless explicitly requested
return 1
fi
read -r -p "Do you want to clean log files? (y/n): " reply
[[ "${reply}" =~ ^[Yy]$ ]]
}
clean_logs() {
if should_clean_logs; then
print_status "Cleaning log files..."
rm -f "${ROOT_DIR}/backend.log" "${ROOT_DIR}/frontend.log" 2>/dev/null || true
print_status "Log files cleaned."
else
print_status "Keeping log files."
fi
}
main() {
stop_services
sleep 1
show_status
clean_logs
echo ""
echo "=========================================="
echo " System stop command completed!"
echo "=========================================="
}
main "$@"