From 752f9054c2ad66262bd4f5cf50ee3e875b7b2422 Mon Sep 17 00:00:00 2001 From: Ryan Whytsell Date: Tue, 21 Oct 2025 15:49:38 -0400 Subject: [PATCH] Updated scripts to use systemd --- DEPLOYMENT.md | 29 +++- start-production.sh | 200 ++++++++++++++++++----- stop-production.sh | 141 +++++++++++----- vidrip.service => vidrip-backend.service | 0 4 files changed, 286 insertions(+), 84 deletions(-) rename vidrip.service => vidrip-backend.service (100%) diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 3e3057c..0d44201 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -10,7 +10,7 @@ The simplest way to run VidRip in production: # Make scripts executable (if not already) chmod +x start-production.sh stop-production.sh -# Start the service +# Start the service (simple mode with nohup) ./start-production.sh # When prompted, enter your domain name (e.g., vidrip.example.com) @@ -20,16 +20,41 @@ chmod +x start-production.sh stop-production.sh ./stop-production.sh ``` +**OR with systemd (recommended for production):** + +```bash +# Start with systemd (auto-starts on boot, better process management) +sudo ./start-production.sh --systemd + +# Stop systemd service +sudo ./stop-production.sh --systemd +``` + The `start-production.sh` script will: - Check system requirements (Node.js 18+, yt-dlp) - Install dependencies - Build backend and frontend - **Ask for your domain and auto-generate Caddyfile** (if not exists) - Deploy frontend to `/var/www/vidrip` -- Start the backend server in the background +- Start the backend server (nohup or systemd) - Create log files in `logs/` directory - Show next steps for Caddy installation +### Deployment Modes + +**Simple Mode (nohup):** +- No sudo required +- Uses PID file for process management +- Logs to `logs/` directory +- Good for testing or simple deployments + +**Systemd Mode (--systemd):** +- Requires sudo +- Auto-starts on system boot +- Better process management and monitoring +- Integration with system logging (`journalctl`) +- Recommended for production servers + ## Production Deployment (Recommended) For a robust production setup, use systemd for process management: diff --git a/start-production.sh b/start-production.sh index 7478cbd..3c401ce 100755 --- a/start-production.sh +++ b/start-production.sh @@ -11,6 +11,30 @@ set -e # Exit on error set -u # Exit on undefined variable +# Parse command line arguments +USE_SYSTEMD=false +while [[ $# -gt 0 ]]; do + case $1 in + --systemd) + USE_SYSTEMD=true + shift + ;; + --help|-h) + echo "Usage: $0 [OPTIONS]" + echo "" + echo "Options:" + echo " --systemd Use systemd service instead of nohup (requires sudo)" + echo " --help Show this help message" + exit 0 + ;; + *) + echo "Unknown option: $1" + echo "Use --help for usage information" + exit 1 + ;; + esac +done + # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' @@ -19,7 +43,8 @@ BLUE='\033[0;34m' NC='\033[0m' # No Color # Script directory -SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +WEB_ROOT="/var/www/vidrip" +SCRIPT_DIR="$WEB_ROOT" cd "$SCRIPT_DIR" # Configuration @@ -29,7 +54,6 @@ LOG_DIR="$SCRIPT_DIR/logs" PID_FILE="$SCRIPT_DIR/vidrip.pid" LOG_FILE="$LOG_DIR/vidrip.log" ERROR_LOG_FILE="$LOG_DIR/vidrip-error.log" -WEB_ROOT="/var/www/vidrip" ################################################################################ # Helper Functions @@ -335,45 +359,116 @@ echo "" log_info "Starting VidRip backend server..." -# Setup environment -export NODE_ENV=production +if [ "$USE_SYSTEMD" = true ]; then + # Systemd mode + log_info "Using systemd service..." -# Start the backend server -cd "$BACKEND_DIR" + # Check if running as root or with sudo + if [ "$EUID" -ne 0 ]; then + log_error "Systemd mode requires root privileges" + log_error "Please run with: sudo $0 --systemd" + exit 1 + fi -# Run in background with output redirected to log files -nohup node dist/server.js > "$LOG_FILE" 2> "$ERROR_LOG_FILE" & -SERVER_PID=$! + # Check if service file exists + SERVICE_FILE="$SCRIPT_DIR/vidrip-backend.service" + if [ ! -f "$SERVICE_FILE" ]; then + log_error "Service file not found: $SERVICE_FILE" + log_error "Please ensure vidrip-backend.service exists in the project directory" + exit 1 + fi -# Save PID -echo "$SERVER_PID" > "$PID_FILE" + # Update service file with correct paths + TEMP_SERVICE="/tmp/vidrip-backend.service" + sed -e "s|/opt/vidrip|$SCRIPT_DIR|g" \ + -e "s|User=vidrip|User=$SUDO_USER|g" \ + -e "s|Group=vidrip|Group=$SUDO_USER|g" \ + -e "s|/usr/bin/node|$(which node)|g" \ + "$SERVICE_FILE" > "$TEMP_SERVICE" -# Wait a moment and check if process is still running -sleep 2 -if ! ps -p "$SERVER_PID" > /dev/null 2>&1; then - log_error "Server failed to start" - log_error "Check logs at:" - log_error " - $LOG_FILE" - log_error " - $ERROR_LOG_FILE" - rm -f "$PID_FILE" - exit 1 -fi + # Install service file + cp "$TEMP_SERVICE" /etc/systemd/system/vidrip-backend.service + rm "$TEMP_SERVICE" -log_success "VidRip backend started successfully!" -echo "" -log_info "Process ID: $SERVER_PID" -log_info "PID file: $PID_FILE" -log_info "Log file: $LOG_FILE" -log_info "Error log: $ERROR_LOG_FILE" -echo "" + log_info "Service file installed to /etc/systemd/system/vidrip-backend.service" + + # Reload systemd + systemctl daemon-reload + + # Enable and start service + systemctl enable vidrip-backend + systemctl restart vidrip-backend + + # Wait and check status + sleep 2 + if systemctl is-active --quiet vidrip-backend; then + log_success "VidRip backend service started successfully!" + echo "" + systemctl status vidrip-backend --no-pager -l + else + log_error "Service failed to start" + log_error "Check status with: sudo systemctl status vidrip-backend" + log_error "View logs with: sudo journalctl -u vidrip-backend -n 50" + exit 1 + fi -# Check for port in logs (default 3001) -sleep 1 -if grep -q "Server running" "$LOG_FILE" 2>/dev/null; then - PORT_INFO=$(grep "Server running" "$LOG_FILE" | tail -1) - log_success "$PORT_INFO" else - log_info "Server should be running on port 3001 (or PORT from .env)" + # Traditional nohup mode + log_info "Using nohup mode..." + + # Check for existing process + if [ -f "$PID_FILE" ]; then + OLD_PID=$(cat "$PID_FILE") + if ps -p "$OLD_PID" > /dev/null 2>&1; then + log_error "VidRip is already running (PID: $OLD_PID)" + log_info "Stop it first with: ./stop-production.sh" + exit 1 + else + log_warning "Stale PID file found, removing..." + rm -f "$PID_FILE" + fi + fi + + # Setup environment + export NODE_ENV=production + + # Start the backend server + cd "$BACKEND_DIR" + + # Run in background with output redirected to log files + nohup node dist/server.js > "$LOG_FILE" 2> "$ERROR_LOG_FILE" & + SERVER_PID=$! + + # Save PID + echo "$SERVER_PID" > "$PID_FILE" + + # Wait a moment and check if process is still running + sleep 2 + if ! ps -p "$SERVER_PID" > /dev/null 2>&1; then + log_error "Server failed to start" + log_error "Check logs at:" + log_error " - $LOG_FILE" + log_error " - $ERROR_LOG_FILE" + rm -f "$PID_FILE" + exit 1 + fi + + log_success "VidRip backend started successfully!" + echo "" + log_info "Process ID: $SERVER_PID" + log_info "PID file: $PID_FILE" + log_info "Log file: $LOG_FILE" + log_info "Error log: $ERROR_LOG_FILE" + echo "" + + # Check for port in logs (default 3001) + sleep 1 + if grep -q "Server running" "$LOG_FILE" 2>/dev/null; then + PORT_INFO=$(grep "Server running" "$LOG_FILE" | tail -1) + log_success "$PORT_INFO" + else + log_info "Server should be running on port 3001 (or PORT from .env)" + fi fi echo "" @@ -383,19 +478,38 @@ log_info "===================================================================" echo "" log_info "Backend Server:" log_info " Running on port 3001 (or PORT from .env)" -log_info " PID: $SERVER_PID" +if [ "$USE_SYSTEMD" = true ]; then + log_info " Mode: systemd service" + log_info " Service: vidrip-backend" +else + log_info " Mode: nohup background process" + log_info " PID: $SERVER_PID" +fi echo "" log_info "Frontend:" log_info " Deployed to: $WEB_ROOT" echo "" -log_info "To stop the backend service:" -log_info " ./stop-production.sh" -echo "" -log_info "To view logs:" -log_info " tail -f $LOG_FILE" -echo "" -log_info "To view errors:" -log_info " tail -f $ERROR_LOG_FILE" + +if [ "$USE_SYSTEMD" = true ]; then + log_info "Service Management:" + log_info " Stop: sudo systemctl stop vidrip-backend" + log_info " Restart: sudo systemctl restart vidrip-backend" + log_info " Status: sudo systemctl status vidrip-backend" + log_info " Disable: sudo systemctl disable vidrip-backend" + echo "" + log_info "View Logs:" + log_info " sudo journalctl -u vidrip-backend -f" + log_info " tail -f $LOG_FILE" +else + log_info "To stop the backend service:" + log_info " ./stop-production.sh" + echo "" + log_info "To view logs:" + log_info " tail -f $LOG_FILE" + echo "" + log_info "To view errors:" + log_info " tail -f $ERROR_LOG_FILE" +fi echo "" if [ -f "$SCRIPT_DIR/Caddyfile" ]; then log_warning "NEXT STEPS - Caddy Setup:" diff --git a/stop-production.sh b/stop-production.sh index fd5b95d..b93bc50 100755 --- a/stop-production.sh +++ b/stop-production.sh @@ -8,6 +8,30 @@ set -e # Exit on error +# Parse command line arguments +USE_SYSTEMD=false +while [[ $# -gt 0 ]]; do + case $1 in + --systemd) + USE_SYSTEMD=true + shift + ;; + --help|-h) + echo "Usage: $0 [OPTIONS]" + echo "" + echo "Options:" + echo " --systemd Stop systemd service instead of PID-based process" + echo " --help Show this help message" + exit 0 + ;; + *) + echo "Unknown option: $1" + echo "Use --help for usage information" + exit 1 + ;; + esac +done + # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' @@ -37,48 +61,87 @@ log_error() { # Stop Service ################################################################################ -if [ ! -f "$PID_FILE" ]; then - log_error "No PID file found at $PID_FILE" - log_error "VidRip may not be running or was started manually" - exit 1 -fi +if [ "$USE_SYSTEMD" = true ]; then + # Systemd mode + log_info "Stopping systemd service..." -PID=$(cat "$PID_FILE") + # Check if running as root or with sudo + if [ "$EUID" -ne 0 ]; then + log_error "Systemd mode requires root privileges" + log_error "Please run with: sudo $0 --systemd" + exit 1 + fi -if ! ps -p "$PID" > /dev/null 2>&1; then - log_error "Process $PID is not running" - log_info "Removing stale PID file..." + # Check if service exists + if ! systemctl list-unit-files | grep -q "vidrip-backend.service"; then + log_error "VidRip backend service is not installed" + log_error "Service may have been started in nohup mode" + log_info "Try running without --systemd flag" + exit 1 + fi + + # Stop the service + systemctl stop vidrip-backend + + # Wait and verify + sleep 1 + if systemctl is-active --quiet vidrip-backend; then + log_error "Failed to stop service" + log_error "Check status with: sudo systemctl status vidrip-backend" + exit 1 + fi + + log_success "VidRip backend service stopped successfully" + log_info "To disable auto-start on boot: sudo systemctl disable vidrip-backend" + log_info "To view logs: sudo journalctl -u vidrip-backend" + +else + # Traditional PID-based stop + if [ ! -f "$PID_FILE" ]; then + log_error "No PID file found at $PID_FILE" + log_error "VidRip may not be running or was started with --systemd" + log_info "Try running with --systemd flag if using systemd service" + exit 1 + fi + + PID=$(cat "$PID_FILE") + + if ! ps -p "$PID" > /dev/null 2>&1; then + log_error "Process $PID is not running" + log_info "Removing stale PID file..." + rm -f "$PID_FILE" + exit 1 + fi + + log_info "Stopping VidRip (PID: $PID)..." + + # Try graceful shutdown first + kill "$PID" + + # Wait up to 10 seconds for graceful shutdown + COUNTER=0 + while ps -p "$PID" > /dev/null 2>&1 && [ $COUNTER -lt 10 ]; do + sleep 1 + COUNTER=$((COUNTER + 1)) + done + + # Force kill if still running + if ps -p "$PID" > /dev/null 2>&1; then + log_info "Process did not stop gracefully, forcing shutdown..." + kill -9 "$PID" + sleep 1 + fi + + # Verify it's stopped + if ps -p "$PID" > /dev/null 2>&1; then + log_error "Failed to stop process $PID" + exit 1 + fi + + # Remove PID file rm -f "$PID_FILE" - exit 1 + + log_success "VidRip stopped successfully" fi -log_info "Stopping VidRip (PID: $PID)..." - -# Try graceful shutdown first -kill "$PID" - -# Wait up to 10 seconds for graceful shutdown -COUNTER=0 -while ps -p "$PID" > /dev/null 2>&1 && [ $COUNTER -lt 10 ]; do - sleep 1 - COUNTER=$((COUNTER + 1)) -done - -# Force kill if still running -if ps -p "$PID" > /dev/null 2>&1; then - log_info "Process did not stop gracefully, forcing shutdown..." - kill -9 "$PID" - sleep 1 -fi - -# Verify it's stopped -if ps -p "$PID" > /dev/null 2>&1; then - log_error "Failed to stop process $PID" - exit 1 -fi - -# Remove PID file -rm -f "$PID_FILE" - -log_success "VidRip stopped successfully" exit 0 diff --git a/vidrip.service b/vidrip-backend.service similarity index 100% rename from vidrip.service rename to vidrip-backend.service