-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstop_bots.py
More file actions
81 lines (70 loc) · 2.3 KB
/
stop_bots.py
File metadata and controls
81 lines (70 loc) · 2.3 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
#!/usr/bin/env python3
"""
Stop all running bot instances safely.
"""
import os
import sys
import subprocess
import signal
from pathlib import Path
SIBLINGS_DIR = Path(__file__).parent
PID_DIR = SIBLINGS_DIR / ".pids"
SARAH_PID_FILE = PID_DIR / "sarah.pid"
ATLAS_PID_FILE = PID_DIR / "atlas.pid"
ARGUS_PID_FILE = PID_DIR / "argus.pid"
def kill_process(pid: int) -> bool:
"""Kill a process by PID."""
try:
if sys.platform == "win32":
subprocess.run(["taskkill", "/F", "/PID", str(pid)],
capture_output=True, check=False)
else:
os.kill(pid, signal.SIGTERM)
return True
except Exception as e:
print(f"Could not kill PID {pid}: {e}")
return False
def stop_bot(pid_file: Path, name: str):
"""Stop a bot by reading its PID file."""
if not pid_file.exists():
print(f"{name}: No PID file found (not running?)")
return
try:
pid = int(pid_file.read_text().strip())
print(f"Stopping {name} (PID {pid})...")
if kill_process(pid):
print(f"{name} stopped.")
pid_file.unlink(missing_ok=True)
except (ValueError, FileNotFoundError) as e:
print(f"{name}: Error reading PID file: {e}")
def kill_all_python_bots():
"""Kill any remaining Python bot processes."""
if sys.platform == "win32":
result = subprocess.run(
["wmic", "process", "where", "name='python.exe'", "get", "processid,commandline"],
capture_output=True, text=True
)
killed = 0
for line in result.stdout.split('\n'):
if 'bot.main' in line:
parts = line.strip().split()
if parts:
try:
pid = int(parts[-1])
kill_process(pid)
killed += 1
except ValueError:
pass
if killed:
print(f"Killed {killed} orphan bot process(es).")
def main():
print("Stopping Siblings bots...")
print("-" * 30)
stop_bot(SARAH_PID_FILE, "Sarah")
stop_bot(ATLAS_PID_FILE, "Atlas")
stop_bot(ARGUS_PID_FILE, "Argus")
print("\nCleaning up any orphan processes...")
kill_all_python_bots()
print("\nAll bots stopped.")
if __name__ == "__main__":
main()