vidrip/stop-production.sh

148 lines
3.8 KiB
Bash
Executable File

#!/bin/bash
################################################################################
# VidRip Production Stop Script
################################################################################
# This script safely stops the VidRip production service
################################################################################
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'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Script directory
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
PID_FILE="$SCRIPT_DIR/vidrip.pid"
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
################################################################################
# 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
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"
log_success "VidRip stopped successfully"
fi
exit 0