#!/bin/bash # Final Fix - Simple Working Version set -euo pipefail RED='\033[0;31m' GREEN='\033[0;32m' BLUE='\033[0;34m' NC='\033[0m' print_status() { echo -e "${GREEN}[FIX]${NC} $1" } print_header() { echo -e "${BLUE}================================${NC}" echo -e "${BLUE} Final PC Monitor Fix${NC}" echo -e "${BLUE}================================${NC}" } if [[ $EUID -ne 0 ]]; then echo "Run with: sudo ./final-fix.sh" exit 1 fi print_header print_status "Creating simple working version..." # Create a much simpler version that actually works cat > /usr/local/bin/pc-monitor << 'EOF' #!/bin/bash set -euo pipefail readonly CONFIG_FILE="/etc/pc-monitor.conf" readonly LOG_FILE="/var/log/pc-monitor.log" # Default thresholds CPU_THRESHOLD=85 MEMORY_THRESHOLD=90 TEMP_THRESHOLD=80 # Load config if [[ -f "$CONFIG_FILE" ]]; then source "$CONFIG_FILE" fi # Simple logging log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE" } # Simple notification that works send_notification() { local title="$1" local message="$2" log "ALERT: $title - $message" # Find user and send notification local active_user=$(who | head -1 | awk '{print $1}') if [[ -n "$active_user" ]]; then sudo -u "$active_user" DISPLAY=:0 notify-send --urgency=critical \ --expire-time=5000 "$title" "$message" 2>/dev/null || true fi } # Monitor functions monitor_system() { # CPU check local cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | sed 's/%us,//' | cut -d. -f1) if [[ "$cpu_usage" -gt "$CPU_THRESHOLD" ]]; then log "HIGH CPU: ${cpu_usage}%" send_notification "⚠️ High CPU Usage" "CPU: ${cpu_usage}% - Killing processes" # Kill top CPU process local top_cpu_pid=$(ps aux --sort=-%cpu | head -2 | tail -1 | awk '{print $2}') if [[ -n "$top_cpu_pid" ]]; then local process_name=$(ps -p "$top_cpu_pid" -o comm= 2>/dev/null || echo "unknown") if kill -9 "$top_cpu_pid" 2>/dev/null; then log "KILLED: PID=$top_cpu_pid NAME=$process_name" fi fi fi # Memory check local mem_percent=$(free | grep '^Mem:' | awk '{printf "%.0f", ($3/$2) * 100}') if [[ "$mem_percent" -gt "$MEMORY_THRESHOLD" ]]; then log "HIGH MEMORY: ${mem_percent}%" send_notification "⚠️ High Memory Usage" "RAM: ${mem_percent}% - Killing processes" # Kill top memory process local top_mem_pid=$(ps aux --sort=-%mem | head -2 | tail -1 | awk '{print $2}') if [[ -n "$top_mem_pid" ]]; then local process_name=$(ps -p "$top_mem_pid" -o comm= 2>/dev/null || echo "unknown") if kill -9 "$top_mem_pid" 2>/dev/null; then log "KILLED: PID=$top_mem_pid NAME=$process_name" fi fi fi # Temperature check if command -v sensors >/dev/null 2>&1; then local temp=$(sensors 2>/dev/null | grep -i "core\|cpu" | grep "°C" | head -1 | grep -o '+[0-9]*' | sed 's/+//' || echo "0") if [[ "$temp" -gt "$TEMP_THRESHOLD" ]]; then log "HIGH TEMP: ${temp}°C" send_notification "🌡️ High Temperature" "Temp: ${temp}°C - Cooling system" fi fi } # Main execution log "PC Monitor started successfully" send_notification "🛡️ PC Monitor Started" "System protection is now active" # Main monitoring loop while true; do monitor_system sleep 5 done EOF chmod +x /usr/local/bin/pc-monitor print_status "Stopping old service..." systemctl stop pc-monitor.service 2>/dev/null || true # Fix the systemd service to point to the working script print_status "Fixing systemd service configuration..." sed -i 's|ExecStart=.*|ExecStart=/usr/local/bin/pc-monitor|' /etc/systemd/system/pc-monitor.service # Remove the problematic security settings sed -i '/ProtectHome=true/d' /etc/systemd/system/pc-monitor.service sed -i '/ProtectSystem=strict/d' /etc/systemd/system/pc-monitor.service # Reload and start print_status "Reloading systemd and starting service..." systemctl daemon-reload systemctl start pc-monitor.service sleep 3 if systemctl is-active --quiet pc-monitor.service; then print_status "✅ SUCCESS! PC Monitor is now running!" echo systemctl status pc-monitor.service --no-pager --lines=3 echo print_status "🛡️ Your system is protected!" else echo "❌ Still having issues. Let's check manually:" /usr/local/bin/pc-monitor & echo "Started manually in background" fi