diff --git a/.gitignore b/.gitignore index 5bb80a6..8d2aa11 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ __pycache__/ *.py[cod] *$py.class *.log +*.pid + # Environments .env diff --git a/README.md b/README.md index 5802aa6..921d2aa 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,19 @@ An automated, AI-powered media organizer with in-browser video streaming, multi- - CIFS/SMB share mounted or access to qBittorrent WebUI. ### 2. Start the Server + +**Foreground:** ```bash python3 server.py ``` +**Background (Silent Mode):** +```bash +./start.sh # Start in background (logs to server.log) +./stop.sh # Stop running server +./restart.sh # Restart server +``` + ### 3. Open in Browser Visit **`http://localhost:8000`** (or your machine's IP on port 8000). diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4530a7c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +# Python Standard Library only +# Requires Python >= 3.8 +# No external third-party packages required diff --git a/restart.sh b/restart.sh new file mode 100755 index 0000000..1acfba4 --- /dev/null +++ b/restart.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +"$DIR/stop.sh" +sleep 1 +"$DIR/start.sh" diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..cfda8cd --- /dev/null +++ b/start.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -e + +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +cd "$DIR" + +PID_FILE="$DIR/server.pid" +LOG_FILE="$DIR/server.log" + +if [ -f "$PID_FILE" ]; then + PID=$(cat "$PID_FILE") + if kill -0 "$PID" 2>/dev/null; then + echo "Server is already running with PID $PID." + exit 0 + else + rm -f "$PID_FILE" + fi +fi + +# Start server in detached session, redirecting output to log file +setsid python3 -u server.py > "$LOG_FILE" 2>&1 < /dev/null & +PID=$! +echo $PID > "$PID_FILE" + + +# Quick check if it started successfully +sleep 0.5 +if kill -0 "$PID" 2>/dev/null; then + echo "Server started successfully (PID $PID)." + echo "Logs: $LOG_FILE" +else + echo "Failed to start server. Check logs: $LOG_FILE" + rm -f "$PID_FILE" + exit 1 +fi diff --git a/stop.sh b/stop.sh new file mode 100755 index 0000000..6523cc6 --- /dev/null +++ b/stop.sh @@ -0,0 +1,45 @@ +#!/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"