forked from d1str4ught/m2dev-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop.py
More file actions
70 lines (60 loc) · 1.74 KB
/
stop.py
File metadata and controls
70 lines (60 loc) · 1.74 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
import os
import sys
import time
import json
import subprocess
import signal
import traceback
GAMEDIR = os.getcwd()
PIDS_FILE = os.path.join(GAMEDIR, "pids.json")
def print_green(text):
print("\033[1;32m" + text + "\033[0m")
def stop_pid(pid, name):
try:
if os.name == "nt":
subprocess.call(["taskkill", "/F", "/PID", str(pid)],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
os.kill(pid, signal.SIGTERM)
except ProcessLookupError:
print(f"> Process {pid} ({name}) not found, skipping.")
except Exception as e:
print(f"> Error stopping {name} (PID {pid}): {e}")
traceback.print_exc()
def kill_by_name(name):
try:
if os.name == "nt":
subprocess.call(["taskkill", "/F", "/IM", f"{name}.exe"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
else:
subprocess.call(["pkill", "-1", name],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
except Exception as e:
print(f"> Error killing {name} by name: {e}")
traceback.print_exc()
def main():
try:
with open(PIDS_FILE, "r") as f:
entries = json.load(f)
except Exception as e:
print(f"> Could not read PID file: {e}")
traceback.print_exc()
sys.exit(1)
for entry in entries.get("channel", []):
name = entry.get("name")
pid = entry.get("pid")
print_green(f"> Stopping {name} (PID {pid})...")
stop_pid(pid, name)
time.sleep(0.2)
auth = entries.get("auth")
if auth:
print_green(f"> Stopping {auth.get('name')} (PID {auth.get('pid')})...")
stop_pid(auth.get('pid'), auth.get('name'))
time.sleep(1)
db = entries.get("db")
if db:
print_green(f"> Stopping {db.get('name')} (PID {db.get('pid')})...")
stop_pid(db.get('pid'), db.get('name'))
print_green("> All requested processes signaled.")
if __name__ == "__main__":
main()