Updated scripts to use systemd

This commit is contained in:
Ryan Whytsell 2025-10-21 15:49:38 -04:00
parent a8a6390a55
commit 752f9054c2
Signed by: Epithium
GPG Key ID: 940AC18C08E925EA
4 changed files with 286 additions and 84 deletions

View File

@ -10,7 +10,7 @@ The simplest way to run VidRip in production:
# Make scripts executable (if not already) # Make scripts executable (if not already)
chmod +x start-production.sh stop-production.sh chmod +x start-production.sh stop-production.sh
# Start the service # Start the service (simple mode with nohup)
./start-production.sh ./start-production.sh
# When prompted, enter your domain name (e.g., vidrip.example.com) # 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 ./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: The `start-production.sh` script will:
- Check system requirements (Node.js 18+, yt-dlp) - Check system requirements (Node.js 18+, yt-dlp)
- Install dependencies - Install dependencies
- Build backend and frontend - Build backend and frontend
- **Ask for your domain and auto-generate Caddyfile** (if not exists) - **Ask for your domain and auto-generate Caddyfile** (if not exists)
- Deploy frontend to `/var/www/vidrip` - 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 - Create log files in `logs/` directory
- Show next steps for Caddy installation - 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) ## Production Deployment (Recommended)
For a robust production setup, use systemd for process management: For a robust production setup, use systemd for process management:

View File

@ -11,6 +11,30 @@
set -e # Exit on error set -e # Exit on error
set -u # Exit on undefined variable 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 # Color codes for output
RED='\033[0;31m' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
@ -19,7 +43,8 @@ BLUE='\033[0;34m'
NC='\033[0m' # No Color NC='\033[0m' # No Color
# Script directory # Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" WEB_ROOT="/var/www/vidrip"
SCRIPT_DIR="$WEB_ROOT"
cd "$SCRIPT_DIR" cd "$SCRIPT_DIR"
# Configuration # Configuration
@ -29,7 +54,6 @@ LOG_DIR="$SCRIPT_DIR/logs"
PID_FILE="$SCRIPT_DIR/vidrip.pid" PID_FILE="$SCRIPT_DIR/vidrip.pid"
LOG_FILE="$LOG_DIR/vidrip.log" LOG_FILE="$LOG_DIR/vidrip.log"
ERROR_LOG_FILE="$LOG_DIR/vidrip-error.log" ERROR_LOG_FILE="$LOG_DIR/vidrip-error.log"
WEB_ROOT="/var/www/vidrip"
################################################################################ ################################################################################
# Helper Functions # Helper Functions
@ -335,6 +359,76 @@ echo ""
log_info "Starting VidRip backend server..." log_info "Starting VidRip backend server..."
if [ "$USE_SYSTEMD" = true ]; then
# Systemd mode
log_info "Using systemd service..."
# 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
# 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
# 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"
# Install service file
cp "$TEMP_SERVICE" /etc/systemd/system/vidrip-backend.service
rm "$TEMP_SERVICE"
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
else
# 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 # Setup environment
export NODE_ENV=production export NODE_ENV=production
@ -375,6 +469,7 @@ if grep -q "Server running" "$LOG_FILE" 2>/dev/null; then
else else
log_info "Server should be running on port 3001 (or PORT from .env)" log_info "Server should be running on port 3001 (or PORT from .env)"
fi fi
fi
echo "" echo ""
log_info "===================================================================" log_info "==================================================================="
@ -383,11 +478,29 @@ log_info "==================================================================="
echo "" echo ""
log_info "Backend Server:" log_info "Backend Server:"
log_info " Running on port 3001 (or PORT from .env)" log_info " Running on port 3001 (or PORT from .env)"
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" log_info " PID: $SERVER_PID"
fi
echo "" echo ""
log_info "Frontend:" log_info "Frontend:"
log_info " Deployed to: $WEB_ROOT" log_info " Deployed to: $WEB_ROOT"
echo "" echo ""
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 "To stop the backend service:"
log_info " ./stop-production.sh" log_info " ./stop-production.sh"
echo "" echo ""
@ -396,6 +509,7 @@ log_info " tail -f $LOG_FILE"
echo "" echo ""
log_info "To view errors:" log_info "To view errors:"
log_info " tail -f $ERROR_LOG_FILE" log_info " tail -f $ERROR_LOG_FILE"
fi
echo "" echo ""
if [ -f "$SCRIPT_DIR/Caddyfile" ]; then if [ -f "$SCRIPT_DIR/Caddyfile" ]; then
log_warning "NEXT STEPS - Caddy Setup:" log_warning "NEXT STEPS - Caddy Setup:"

View File

@ -8,6 +8,30 @@
set -e # Exit on error 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 # Color codes for output
RED='\033[0;31m' RED='\033[0;31m'
GREEN='\033[0;32m' GREEN='\033[0;32m'
@ -37,9 +61,46 @@ log_error() {
# Stop Service # Stop Service
################################################################################ ################################################################################
if [ "$USE_SYSTEMD" = true ]; then
# Systemd mode
log_info "Stopping systemd service..."
# 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
# 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 if [ ! -f "$PID_FILE" ]; then
log_error "No PID file found at $PID_FILE" log_error "No PID file found at $PID_FILE"
log_error "VidRip may not be running or was started manually" 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 exit 1
fi fi
@ -81,4 +142,6 @@ fi
rm -f "$PID_FILE" rm -f "$PID_FILE"
log_success "VidRip stopped successfully" log_success "VidRip stopped successfully"
fi
exit 0 exit 0