-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstop.sh
More file actions
executable file
·35 lines (30 loc) · 854 Bytes
/
stop.sh
File metadata and controls
executable file
·35 lines (30 loc) · 854 Bytes
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
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PID_FILE="$SCRIPT_DIR/.server.pid"
PORT="${PORT:-3001}"
stopped=0
# --- 1. Kill via PID file if it exists ---
if [[ -f "$PID_FILE" ]]; then
PID="$(cat "$PID_FILE")"
if kill -0 "$PID" 2>/dev/null; then
kill "$PID"
echo "Stopped server (PID $PID from .server.pid)."
stopped=1
fi
rm -f "$PID_FILE"
fi
# --- 2. Kill anything still holding the port (handles manually-started servers) ---
PORT_PIDS="$(fuser "${PORT}/tcp" 2>/dev/null || true)"
if [[ -n "$PORT_PIDS" ]]; then
for pid in $PORT_PIDS; do
if kill -0 "$pid" 2>/dev/null; then
kill "$pid"
echo "Stopped server (PID $pid was holding port $PORT)."
stopped=1
fi
done
fi
if [[ $stopped -eq 0 ]]; then
echo "No server found running on port $PORT."
fi