-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstop
More file actions
executable file
·141 lines (122 loc) · 3.94 KB
/
Copy pathstop
File metadata and controls
executable file
·141 lines (122 loc) · 3.94 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
#!/bin/bash
# Stop CampaignGenerator server(s) started by ./start.
#
# Usage:
# ./stop # stop tracked instances (via server*.pid files)
# ./stop --port 5000 # stop only the instance on port 5000
# ./stop --all # also sweep and kill stray `python -m server.main`
# # processes that no PID file knows about
# ./stop --all --port 5000 # sweep narrowed to processes on port 5000
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# Parse --port and --all
TARGET_PORT=""
SWEEP_ALL=false
PREV=""
for ARG in "$@"; do
if [ "$PREV" = "--port" ]; then
TARGET_PORT="$ARG"
fi
if [ "$ARG" = "--all" ]; then
SWEEP_ALL=true
fi
PREV="$ARG"
done
# Collect PID files to process
if [ -n "$TARGET_PORT" ]; then
PID_FILES=("$SCRIPT_DIR/server-${TARGET_PORT}.pid")
# Also check legacy server.pid
if [ "$TARGET_PORT" = "5000" ] && [ -f "$SCRIPT_DIR/server.pid" ]; then
PID_FILES+=("$SCRIPT_DIR/server.pid")
fi
else
# Stop all instances — gather all server*.pid files
PID_FILES=()
for f in "$SCRIPT_DIR"/server*.pid; do
[ -f "$f" ] && PID_FILES+=("$f")
done
fi
if [ ${#PID_FILES[@]} -eq 0 ] && ! $SWEEP_ALL; then
echo "No PID files found — is a server running?"
echo "If a server was started outside ./start, try ./stop --all to sweep."
exit 1
fi
FOUND_ANY=false
for PID_FILE in "${PID_FILES[@]}"; do
[ -f "$PID_FILE" ] || continue
PID="$(cat "$PID_FILE")"
LABEL="$(basename "$PID_FILE" .pid)"
if ! kill -0 "$PID" 2>/dev/null; then
echo "Process $PID ($LABEL) not found. Cleaning up stale PID file."
rm -f "$PID_FILE"
continue
fi
FOUND_ANY=true
echo "Stopping $LABEL (PID $PID)..."
kill "$PID"
for i in $(seq 1 10); do
if ! kill -0 "$PID" 2>/dev/null; then
rm -f "$PID_FILE"
echo " Stopped."
break
fi
sleep 0.5
done
if kill -0 "$PID" 2>/dev/null; then
echo " Didn't stop cleanly — sending SIGKILL."
kill -9 "$PID" 2>/dev/null
rm -f "$PID_FILE"
echo " Killed."
fi
done
if ! $FOUND_ANY; then
echo "No tracked servers found via PID files."
fi
# Stray-process sweep only runs when --all is passed. Without --all, only
# PID-file-tracked processes are stopped. Strays are processes started via
# ./startup directly, or left over from a crashed ./start.
if ! $SWEEP_ALL; then
# Hint the user if strays exist so they know --all is an option.
STRAY_COUNT="$(pgrep -c -f 'python.* -m server\.main' 2>/dev/null || echo 0)"
if [ "$STRAY_COUNT" -gt 0 ]; then
echo
echo "Note: $STRAY_COUNT untracked 'python -m server.main' process(es) still running."
echo "Run './stop --all' to sweep them too."
fi
exit 0
fi
SWEEP_PATTERN='python.* -m server\.main'
if [ -n "$TARGET_PORT" ]; then
# Narrow the sweep to matches that mention the requested port.
SWEEP_PATTERN="python.* -m server\.main.*--port[= ]$TARGET_PORT"
fi
SELF_PID=$$
STRAY_PIDS=()
while IFS= read -r PID; do
[ -z "$PID" ] && continue
[ "$PID" = "$SELF_PID" ] && continue
STRAY_PIDS+=("$PID")
done < <(pgrep -f "$SWEEP_PATTERN" 2>/dev/null || true)
if [ ${#STRAY_PIDS[@]} -gt 0 ]; then
echo
echo "Found ${#STRAY_PIDS[@]} stray server process(es):"
for PID in "${STRAY_PIDS[@]}"; do
CMD="$(ps -p "$PID" -o args= 2>/dev/null || echo '<gone>')"
echo " PID $PID: $CMD"
done
for PID in "${STRAY_PIDS[@]}"; do
echo "Stopping stray PID $PID..."
kill "$PID" 2>/dev/null || continue
for i in $(seq 1 10); do
if ! kill -0 "$PID" 2>/dev/null; then
echo " Stopped."
break
fi
sleep 0.5
done
if kill -0 "$PID" 2>/dev/null; then
echo " Didn't stop cleanly — sending SIGKILL."
kill -9 "$PID" 2>/dev/null
echo " Killed."
fi
done
fi