Updated scripts to use systemd
This commit is contained in:
parent
a8a6390a55
commit
752f9054c2
|
|
@ -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:
|
||||||
|
|
|
||||||
|
|
@ -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,45 +359,116 @@ echo ""
|
||||||
|
|
||||||
log_info "Starting VidRip backend server..."
|
log_info "Starting VidRip backend server..."
|
||||||
|
|
||||||
# Setup environment
|
if [ "$USE_SYSTEMD" = true ]; then
|
||||||
export NODE_ENV=production
|
# Systemd mode
|
||||||
|
log_info "Using systemd service..."
|
||||||
|
|
||||||
# Start the backend server
|
# Check if running as root or with sudo
|
||||||
cd "$BACKEND_DIR"
|
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
|
# Check if service file exists
|
||||||
nohup node dist/server.js > "$LOG_FILE" 2> "$ERROR_LOG_FILE" &
|
SERVICE_FILE="$SCRIPT_DIR/vidrip-backend.service"
|
||||||
SERVER_PID=$!
|
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
|
# Update service file with correct paths
|
||||||
echo "$SERVER_PID" > "$PID_FILE"
|
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
|
# Install service file
|
||||||
sleep 2
|
cp "$TEMP_SERVICE" /etc/systemd/system/vidrip-backend.service
|
||||||
if ! ps -p "$SERVER_PID" > /dev/null 2>&1; then
|
rm "$TEMP_SERVICE"
|
||||||
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!"
|
log_info "Service file installed to /etc/systemd/system/vidrip-backend.service"
|
||||||
echo ""
|
|
||||||
log_info "Process ID: $SERVER_PID"
|
# Reload systemd
|
||||||
log_info "PID file: $PID_FILE"
|
systemctl daemon-reload
|
||||||
log_info "Log file: $LOG_FILE"
|
|
||||||
log_info "Error log: $ERROR_LOG_FILE"
|
# Enable and start service
|
||||||
echo ""
|
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
|
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
|
fi
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
|
@ -383,19 +478,38 @@ 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)"
|
||||||
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 ""
|
echo ""
|
||||||
log_info "Frontend:"
|
log_info "Frontend:"
|
||||||
log_info " Deployed to: $WEB_ROOT"
|
log_info " Deployed to: $WEB_ROOT"
|
||||||
echo ""
|
echo ""
|
||||||
log_info "To stop the backend service:"
|
|
||||||
log_info " ./stop-production.sh"
|
if [ "$USE_SYSTEMD" = true ]; then
|
||||||
echo ""
|
log_info "Service Management:"
|
||||||
log_info "To view logs:"
|
log_info " Stop: sudo systemctl stop vidrip-backend"
|
||||||
log_info " tail -f $LOG_FILE"
|
log_info " Restart: sudo systemctl restart vidrip-backend"
|
||||||
echo ""
|
log_info " Status: sudo systemctl status vidrip-backend"
|
||||||
log_info "To view errors:"
|
log_info " Disable: sudo systemctl disable vidrip-backend"
|
||||||
log_info " tail -f $ERROR_LOG_FILE"
|
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 ""
|
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:"
|
||||||
|
|
|
||||||
|
|
@ -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,48 +61,87 @@ log_error() {
|
||||||
# Stop Service
|
# Stop Service
|
||||||
################################################################################
|
################################################################################
|
||||||
|
|
||||||
if [ ! -f "$PID_FILE" ]; then
|
if [ "$USE_SYSTEMD" = true ]; then
|
||||||
log_error "No PID file found at $PID_FILE"
|
# Systemd mode
|
||||||
log_error "VidRip may not be running or was started manually"
|
log_info "Stopping systemd service..."
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
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
|
# Check if service exists
|
||||||
log_error "Process $PID is not running"
|
if ! systemctl list-unit-files | grep -q "vidrip-backend.service"; then
|
||||||
log_info "Removing stale PID file..."
|
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"
|
rm -f "$PID_FILE"
|
||||||
exit 1
|
|
||||||
|
log_success "VidRip stopped successfully"
|
||||||
fi
|
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
|
exit 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue