-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathshutdown_all.py
More file actions
353 lines (296 loc) · 11 KB
/
Copy pathshutdown_all.py
File metadata and controls
353 lines (296 loc) · 11 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
"""
Artemis Graceful Shutdown
========================
Closes ALL project processes: llama-server, Live2D bridge, ComfyUI,
OpenClaw Gateway, Artemis Bridge, Task Board, WebChat, Sakura Desktop Pet,
Embedding Server, and runs cleanup_orphans.ps1.
Usage:
python shutdown_all.py # full output
python shutdown_all.py --quiet # minimal output
Exit codes:
0 = all clean
1 = some processes failed to stop
"""
import sys
import os
import subprocess
import socket
import time
import argparse
if sys.platform == "win32":
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
sys.stderr.reconfigure(encoding="utf-8", errors="replace")
WORKSPACE = os.path.dirname(os.path.abspath(__file__))
# ---- Helpers ----
def port_open(host, port, timeout=1):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(timeout)
try:
s.connect((host, port))
return True
except (socket.timeout, ConnectionRefusedError, OSError):
return False
finally:
s.close()
def kill_by_port(port, name, wait_sec=3):
"""Kill the process owning a local port via netstat + taskkill (no admin needed)."""
if not port_open("127.0.0.1", port):
print(f"[shutdown] {name}: not running")
return True
print(f"[shutdown] {name}: stopping (port {port})...")
# Find PID via netstat (works without admin)
pid = _get_pid_by_port(port)
if pid:
_force_kill_pid(pid, name)
else:
# Fallback: taskkill by port
subprocess.run(
["powershell", "-NoProfile", "-Command",
f"Get-NetTCPConnection -LocalPort {port} -ErrorAction SilentlyContinue | "
"ForEach-Object { Stop-Process -Id $_.OwningProcess -Force -ErrorAction SilentlyContinue }"],
capture_output=True, text=True, timeout=8
)
for i in range(wait_sec * 2):
time.sleep(0.5)
if not port_open("127.0.0.1", port):
print(f"[shutdown] {name}: stopped ({(i+1)*0.5:.0f}s)")
return True
# Aggressive: try again with taskkill
pid = _get_pid_by_port(port)
if pid:
_force_kill_pid(pid, name, force=True)
time.sleep(1)
if not port_open("127.0.0.1", port):
print(f"[shutdown] {name}: force killed")
return True
print(f"[shutdown] {name}: STILL RUNNING — manual kill needed")
return False
def _get_pid_by_port(port):
"""Get PID listening on port using netstat (no admin required)."""
try:
result = subprocess.run(
["netstat", "-ano"], capture_output=True, text=True, timeout=5,
encoding="utf-8", errors="replace"
)
for line in result.stdout.splitlines():
if f":{port}" in line and "LISTENING" in line:
parts = line.strip().split()
pid = parts[-1]
if pid.isdigit():
return int(pid)
except Exception:
pass
return None
def _force_kill_pid(pid, name, force=False):
# Always use /f — most services don't respond to graceful SIGTERM
try:
subprocess.run(
["taskkill", "/f", "/pid", str(pid)],
capture_output=True, timeout=5
)
except Exception:
pass
# ---- Service-specific killers ----
def kill_llama():
if not port_open("127.0.0.1", 8080):
print("[shutdown] llama-server: not running")
return True
print("[shutdown] llama-server: stopping...")
try:
import urllib.request
urllib.request.urlopen("http://127.0.0.1:8080/shutdown", timeout=3)
print("[shutdown] llama-server: /shutdown sent")
except Exception:
print("[shutdown] llama-server: HTTP shutdown failed, force killing")
for i in range(15):
time.sleep(0.5)
if not port_open("127.0.0.1", 8080):
print(f"[shutdown] llama-server: stopped ({i*0.5:.0f}s)")
return True
subprocess.run(["taskkill", "/f", "/im", "llama-server.exe"], capture_output=True)
time.sleep(1)
ok = not port_open("127.0.0.1", 8080)
if ok:
print("[shutdown] llama-server: killed")
else:
print("[shutdown] llama-server: STILL RUNNING — manual kill needed")
return ok
def kill_live2d():
return kill_by_port(19200, "live2d bridge", wait_sec=3)
def kill_comfyui():
if not port_open("127.0.0.1", 8188):
print("[shutdown] ComfyUI: not running")
return True
print("[shutdown] ComfyUI: stopping...")
try:
subprocess.run(
["powershell", "-NoProfile", "-Command",
"Get-CimInstance Win32_Process -Filter \"Name='python.exe'\" | "
"Where-Object { $_.CommandLine -match 'comfyui|ComfyUI|main\\.py' } | "
"ForEach-Object { Stop-Process -Id $_.ProcessId -Force }"],
capture_output=True, text=True, timeout=10
)
except Exception:
pass
for i in range(15):
time.sleep(0.5)
if not port_open("127.0.0.1", 8188):
print(f"[shutdown] ComfyUI: stopped ({i*0.5:.0f}s)")
return True
print("[shutdown] ComfyUI: STILL RUNNING — manual kill needed")
return False
def kill_gateway():
cli = _find_openclaw()
if cli:
try:
result = subprocess.run(
[cli, "gateway", "stop"],
capture_output=True, text=True, timeout=30
)
out = (result.stdout.strip() + " " + result.stderr.strip()).strip()
print(f"[shutdown] gateway: {out}" if out else "[shutdown] gateway: stopped")
return True
except subprocess.TimeoutExpired:
print("[shutdown] gateway: TIMEOUT")
return False
except Exception as e:
print(f"[shutdown] gateway: error: {e}")
return False
else:
print("[shutdown] gateway: CLI not found, trying process kill...")
try:
subprocess.run(
["powershell", "-NoProfile", "-Command",
"Get-Process node -ErrorAction SilentlyContinue | "
"Where-Object { $_.CommandLine -match 'gateway' } | Stop-Process -Force"],
capture_output=True, timeout=10
)
return True
except Exception:
return False
def kill_webchat():
return kill_by_port(19270, "webchat", wait_sec=3)
def kill_bridge():
return kill_by_port(19250, "artemis bridge", wait_sec=3)
def kill_taskboard():
return kill_by_port(19280, "task board", wait_sec=3)
def kill_embedding_server():
return kill_by_port(9999, "embedding server", wait_sec=3)
def kill_sakura():
"""Stop Sakura Desktop Pet (PySide6 GUI)."""
pidFile = os.path.join(WORKSPACE, "skills", "sakura", ".sakura_pid.txt")
if os.path.exists(pidFile):
try:
with open(pidFile) as f:
pid = int(f.read().strip())
import signal
os.kill(pid, signal.SIGTERM)
print(f"[shutdown] Sakura: sent SIGTERM to PID={pid}")
time.sleep(1)
try:
os.kill(pid, 0)
subprocess.run(["taskkill", "/f", "/pid", str(pid)], capture_output=True)
print(f"[shutdown] Sakura: force killed")
except OSError:
print(f"[shutdown] Sakura: stopped")
os.remove(pidFile)
return True
except Exception as e:
print(f"[shutdown] Sakura: PID file error: {e}")
# Method 2: taskkill known python processes with sakura in args (faster)
try:
result = subprocess.run(
["wmic", "process", "where", "name='python.exe'", "get", "processid,commandline"],
capture_output=True, text=True, timeout=5,
encoding="utf-8", errors="replace"
)
pids_to_kill = []
for line in result.stdout.splitlines():
if "sakura" in line.lower() or "Sakura" in line:
parts = line.strip().split()
if parts:
maybe_pid = parts[-1]
if maybe_pid.isdigit():
pids_to_kill.append(maybe_pid)
if pids_to_kill:
for p in pids_to_kill:
subprocess.run(["taskkill", "/f", "/pid", p], capture_output=True)
print(f"[shutdown] Sakura: killed {len(pids_to_kill)} process(es)")
return True
except Exception:
pass
print("[shutdown] Sakura: not running")
return True
def _find_openclaw():
candidates = [
os.path.expandvars(r"%APPDATA%\npm\openclaw.cmd"),
os.path.expandvars(r"%ProgramFiles%\nodejs\openclaw.cmd"),
]
for c in candidates:
if os.path.exists(c):
return c
try:
r = subprocess.run(["where", "openclaw"], capture_output=True, text=True, timeout=5)
if r.returncode == 0 and r.stdout.strip():
return r.stdout.strip().splitlines()[0]
except Exception:
pass
return None
def run_cleanup():
ps1 = os.path.join(WORKSPACE, "skills", "cleanup_orphans.ps1")
if not os.path.exists(ps1):
print("[shutdown] cleanup_orphans: script not found, skipping")
return True
print("[shutdown] cleanup_orphans: running...")
try:
result = subprocess.run(
["powershell", "-ExecutionPolicy", "Bypass", "-NoProfile",
"-File", ps1, "-MaxAgeSeconds", "0"],
capture_output=True, text=True, timeout=60,
encoding="utf-8", errors="replace"
)
lines = [l for l in result.stdout.splitlines() if l.strip()]
for line in lines[-6:]:
print(f" {line}")
print("[shutdown] cleanup_orphans: done")
return True
except subprocess.TimeoutExpired:
print("[shutdown] cleanup_orphans: TIMEOUT")
return False
except Exception as e:
print(f"[shutdown] cleanup_orphans: error: {e}")
return False
# ---- Main ----
def main():
parser = argparse.ArgumentParser(description="Artemis Graceful Shutdown")
parser.add_argument("--quiet", action="store_true")
args = parser.parse_args()
if not args.quiet:
print("=" * 50)
print(" Artemis Shutdown")
print("=" * 50)
print()
results = {}
# Order: thread-level (daemon-managed) first, then standalone processes
results["webchat"] = kill_webchat()
results["taskboard"] = kill_taskboard()
results["bridge"] = kill_bridge()
results["sakura"] = kill_sakura()
results["live2d"] = kill_live2d()
results["embedding"] = kill_embedding_server()
results["llama"] = kill_llama()
results["comfyui"] = kill_comfyui()
results["gateway"] = kill_gateway()
results["cleanup"] = run_cleanup()
print()
all_ok = all(results.values())
if all_ok:
print("[shutdown] All clean. Goodbye!")
else:
failed = [k for k, v in results.items() if not v]
print(f"[shutdown] WARNING: failed to stop: {', '.join(failed)}")
print("[shutdown] You may need to manually kill remaining processes.")
return 0 if all_ok else 1
if __name__ == "__main__":
sys.exit(main())