#!/bin/bash # Resource Monitor Service - Installation Script for Linux systemd service SERVICE_NAME="resource-monitor" SERVICE_DISPLAY_NAME="Resource Monitor Service v2.1" SERVICE_DESCRIPTION="Monitors system resources with web dashboard" INSTALL_PATH="/opt/resource-monitor" SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service" echo "=== Resource Monitor Service - Linux systemd Installer ===" echo # Check if running as root if [[ $EUID -ne 0 ]]; then echo "ERROR: This script must be run as root (use sudo)" echo "Please run: sudo ./install-service-linux.sh" exit 1 fi # Function to uninstall service uninstall_service() { echo "Uninstalling Resource Monitor Service..." # Stop the service echo "Stopping service..." systemctl stop $SERVICE_NAME 2>/dev/null echo "Service stopped" # Disable the service echo "Disabling service..." systemctl disable $SERVICE_NAME 2>/dev/null echo "Service disabled" # Remove service file echo "Removing service file..." rm -f $SERVICE_FILE echo "Service file removed" # Reload systemd systemctl daemon-reload # Optionally remove installation directory read -p "Remove installation files from $INSTALL_PATH? (y/N): " removeFiles if [[ "$removeFiles" == "y" || "$removeFiles" == "Y" ]]; then rm -rf $INSTALL_PATH echo "Installation files removed" fi echo "Uninstallation complete!" exit 0 } # Check for uninstall flag if [[ "$1" == "--uninstall" || "$1" == "-u" ]]; then uninstall_service fi echo "Installing Resource Monitor Service as systemd service..." # Check if .NET is installed if ! command -v dotnet &> /dev/null; then echo "ERROR: .NET runtime is not installed" echo "Please install .NET 8.0 or later runtime" echo "See: https://docs.microsoft.com/en-us/dotnet/core/install/linux" exit 1 fi # Create installation directory echo "Creating installation directory..." mkdir -p "$INSTALL_PATH" if [[ $? -ne 0 ]]; then echo "ERROR: Failed to create installation directory" exit 1 fi echo "Installation directory created: $INSTALL_PATH" # Build the service in release mode echo "Building service..." dotnet publish --configuration Release --output "$INSTALL_PATH" if [[ $? -ne 0 ]]; then echo "ERROR: Build failed" exit 1 fi echo "Service built successfully" # Stop existing service if running echo "Stopping existing service (if running)..." systemctl stop $SERVICE_NAME 2>/dev/null || echo "No existing service found" # Create systemd service file echo "Creating systemd service file..." cat > $SERVICE_FILE << EOF [Unit] Description=$SERVICE_DESCRIPTION After=network.target [Service] Type=notify ExecStart=/usr/bin/dotnet $INSTALL_PATH/ResourceMonitorService.dll Restart=always RestartSec=5 SyslogIdentifier=resource-monitor User=www-data Environment=ASPNETCORE_ENVIRONMENT=Production Environment=ASPNETCORE_URLS=http://localhost:5000 WorkingDirectory=$INSTALL_PATH [Install] WantedBy=multi-user.target EOF if [[ $? -ne 0 ]]; then echo "ERROR: Failed to create service file" exit 1 fi echo "Service file created" # Set proper permissions chmod 644 $SERVICE_FILE chown root:root $SERVICE_FILE # Reload systemd echo "Reloading systemd daemon..." systemctl daemon-reload # Enable the service echo "Enabling service..." systemctl enable $SERVICE_NAME if [[ $? -ne 0 ]]; then echo "ERROR: Failed to enable service" exit 1 fi echo "Service enabled" # Start the service echo "Starting service..." systemctl start $SERVICE_NAME if [[ $? -ne 0 ]]; then echo "ERROR: Failed to start service" echo "Check service status with: systemctl status $SERVICE_NAME" exit 1 fi echo "Service started successfully" # Wait a moment and check service status sleep 3 SERVICE_STATUS=$(systemctl is-active $SERVICE_NAME) echo "Service Status: $SERVICE_STATUS" # Configure firewall (if ufw is available) if command -v ufw &> /dev/null; then echo "Configuring firewall for web dashboard..." ufw allow 5000/tcp comment "Resource Monitor Service" echo "Firewall rule created for port 5000" fi echo echo "=== Installation Complete ===" echo "Service Name: $SERVICE_NAME" echo "Installation Path: $INSTALL_PATH" echo "Web Dashboard: http://localhost:5000" echo "API Documentation: http://localhost:5000/swagger" echo "API Health Check: http://localhost:5000/api/health" echo echo "The service is now running and will start automatically with the system." echo "You can manage it using systemctl commands:" echo " - Stop: sudo systemctl stop $SERVICE_NAME" echo " - Start: sudo systemctl start $SERVICE_NAME" echo " - Status: systemctl status $SERVICE_NAME" echo " - Restart: sudo systemctl restart $SERVICE_NAME" echo " - Logs: journalctl -u $SERVICE_NAME -f" echo echo "To uninstall: sudo ./install-service-linux.sh --uninstall" # Test the web dashboard echo echo "Testing web dashboard..." sleep 5 if command -v curl &> /dev/null; then RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:5000/api/health) if [[ "$RESPONSE" == "200" ]]; then echo "Web Dashboard Test: SUCCESS" echo echo "🎉 Web Dashboard is ready at: http://localhost:5000" echo "📖 API Documentation at: http://localhost:5000/swagger" else echo "Web Dashboard Test: FAILED (HTTP $RESPONSE)" echo "The service may still be starting up. Wait a few minutes and try accessing:" echo "http://localhost:5000" fi else echo "curl not available for testing. Please check manually:" echo "http://localhost:5000" fi