46 lines
1.1 KiB
Bash
Executable File
46 lines
1.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -e
|
|
|
|
DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PID_FILE="$DIR/server.pid"
|
|
|
|
if [ ! -f "$PID_FILE" ]; then
|
|
# Also check if any rogue server.py is running in this dir
|
|
PIDS=$(pgrep -f "python3.*server.py" || true)
|
|
if [ -z "$PIDS" ]; then
|
|
echo "Server is not running."
|
|
exit 0
|
|
else
|
|
echo "PID file not found, but found server process(es): $PIDS. Stopping..."
|
|
kill $PIDS 2>/dev/null || true
|
|
exit 0
|
|
fi
|
|
fi
|
|
|
|
PID=$(cat "$PID_FILE")
|
|
|
|
if kill -0 "$PID" 2>/dev/null; then
|
|
echo "Stopping server (PID $PID)..."
|
|
kill "$PID" 2>/dev/null || true
|
|
|
|
# Wait up to 5 seconds for graceful shutdown
|
|
for i in {1..10}; do
|
|
if ! kill -0 "$PID" 2>/dev/null; then
|
|
break
|
|
fi
|
|
sleep 0.5
|
|
done
|
|
|
|
# Force kill if still alive
|
|
if kill -0 "$PID" 2>/dev/null; then
|
|
echo "Force killing server (PID $PID)..."
|
|
kill -9 "$PID" 2>/dev/null || true
|
|
fi
|
|
|
|
echo "Server stopped."
|
|
else
|
|
echo "Server (PID $PID) was not running."
|
|
fi
|
|
|
|
rm -f "$PID_FILE"
|