-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkill-ports.sh
More file actions
51 lines (44 loc) · 1.45 KB
/
kill-ports.sh
File metadata and controls
51 lines (44 loc) · 1.45 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
#!/bin/sh
# This script finds and kills any processes running on the
# standard AmberOps Console development ports. It first tries `fuser`,
# then falls back to `lsof` for broader compatibility.
set -e
echo "--- Stopping any running development servers ---"
PORTS="3000 3001 3002 3003 3004"
FOUND_PROCESS=0
# Check if fuser is available
if command -v fuser >/dev/null 2>&1; then
echo "Using 'fuser' to terminate processes..."
for PORT in $PORTS; do
if fuser -k -n tcp "$PORT" >/dev/null 2>&1; then
echo "Process on port $PORT found and terminated."
FOUND_PROCESS=1
else
echo "No process found on port $PORT."
fi
done
# Check if lsof is available
elif command -v lsof >/dev/null 2>&1; then
echo "Using 'lsof' as a fallback to terminate processes..."
for PORT in $PORTS; do
# Use lsof to find the PID, -t for terse output (PID only)
PID=$(lsof -t -i:$PORT || true)
if [ -n "$PID" ]; then
echo "Found process with PID $PID on port $PORT. Killing it..."
# Forcefully kill the process
kill -9 "$PID"
echo "Process on port $PORT killed."
FOUND_PROCESS=1
else
echo "No process found on port $PORT."
fi
done
else
echo "Error: Neither 'fuser' nor 'lsof' commands are available. Cannot stop processes."
exit 1
fi
if [ "$FOUND_PROCESS" -eq 1 ]; then
echo "--- All running processes terminated. ---"
else
echo "--- No running processes were found on the specified ports. ---"
fi