#!/bin/bash # ============================================= # Graphical Environment Manager Script # Disables/enables graphical environment to save RAM # Must be run with sudo! # # Usage: # ./gui_manager.sh [enable|disable|status] # GUI_ACTION=enable|disable ./gui_manager.sh # ============================================= # --- Configuration --- LOG_FILE="/var/log/gui_manager.log" MAX_LOG_SIZE_KB=1024 # 1MB # --- Functions --- # Função de Log (modificada para usar logger) log() { local message="$1" local timestamp=$(date '+%Y-%m-%d %H:%M:%S') # Escreve no ficheiro de log echo -e "$timestamp: $message" | tee -a "$LOG_FILE" # Envia para o syslog (opcional) logger -t "gui_manager" "$message" } # Função para limitar o tamanho do log limit_log_size() { local log_file="$1" local max_size_kb="$2" local max_size_bytes=$((max_size_kb * 1024)) if [ -f "$log_file" ]; then local current_size=$(stat -c %s "$log_file" 2>/dev/null || wc -c < "$log_file" 2>/dev/null) if [ "$current_size" -gt "$max_size_bytes" ]; then log "Aviso: Ficheiro de log $log_file excedeu $max_size_kb KB. A truncar..." tail -n 500 "$log_file" > "${log_file}.tmp" && mv "${log_file}.tmp" "$log_file" log "Ficheiro de log truncado. As últimas 500 linhas foram mantidas." fi fi } # Function to check if system uses systemd is_systemd() { [ -d /run/systemd/system ] && return 0 || return 1 } # Function to get current display manager get_display_manager() { if [ -f /etc/X11/default-display-manager ]; then cat /etc/X11/default-display-manager elif systemctl list-units --type=service | grep -q "gdm"; then echo "/usr/sbin/gdm3" elif systemctl list-units --type=service | grep -q "lightdm"; then echo "/usr/sbin/lightdm" elif systemctl list-units --type=service | grep -q "sddm"; then echo "/usr/sbin/sddm" elif systemctl list-units --type=service | grep -q "lxdm"; then echo "/usr/sbin/lxdm" else echo "unknown" fi } # Function to get current target get_current_target() { if is_systemd; then systemctl get-default else log "System doesn't use systemd" return 1 fi } # Function to show GUI status show_gui_status() { if ! is_systemd; then log "System doesn't use systemd" return 1 fi current_target=$(get_current_target) dm=$(get_display_manager) log "Current system target: $current_target" log "Display Manager: $(basename "$dm")" if [ "$current_target" = "graphical.target" ]; then log "Graphical environment is ENABLED" else log "Graphical environment is DISABLED" fi # Show memory usage log "Memory usage:" free -h | awk '/^Mem:/ {log " Total: " $2 ", Used: " $3 ", Free: " $4}' } # Function to disable graphical environment disable_gui() { if ! is_systemd; then log "Error: This system doesn't use systemd" return 1 fi current_target=$(get_current_target) if [ "$current_target" != "graphical.target" ]; then log "Graphical environment is already disabled" return 0 fi log "Disabling graphical environment..." # Get current display manager dm=$(get_display_manager) if [ "$dm" = "unknown" ]; then log "Error: Could not determine display manager" return 1 fi # Show memory usage before log "Memory usage before disabling GUI:" free -h | awk '/^Mem:/ {log " Total: " $2 ", Used: " $3 ", Free: " $4}' # Stop display manager log "Stopping display manager $(basename "$dm")..." if ! systemctl stop "$(basename "$dm")"; then log "Error: Failed to stop display manager $(basename "$dm")" return 1 fi # Disable graphical target log "Setting default target to multi-user.target..." if ! systemctl set-default multi-user.target; then log "Error: Failed to set default target to multi-user.target" return 1 fi # Show memory usage after log "Memory usage after disabling GUI:" free -h | awk '/^Mem:/ {log " Total: " $2 ", Used: " $3 ", Free: " $4}' log "Graphical environment disabled successfully" return 0 } # Function to enable graphical environment enable_gui() { if ! is_systemd; then log "Error: This system doesn't use systemd" return 1 fi current_target=$(get_current_target) if [ "$current_target" = "graphical.target" ]; then log "Graphical environment is already enabled" return 0 fi log "Enabling graphical environment..." # Get current display manager dm=$(get_display_manager) if [ "$dm" = "unknown" ]; then log "Error: Could not determine display manager" return 1 fi # Enable graphical target log "Setting default target to graphical.target..." if ! systemctl set-default graphical.target; then log "Error: Failed to set default target to graphical.target" return 1 fi # Start display manager log "Starting display manager $(basename "$dm")..." if ! systemctl start "$(basename "$dm")"; then log "Error: Failed to start display manager $(basename "$dm")" return 1 fi log "Graphical environment enabled successfully" return 0 } # Function to format free output for logging format_free_output() { free -h | awk '/^Mem:/ { log " Total: " $2 ", Used: " $3 ", Free: " $4 }' } # --- Main Execution --- # Check if running as root if [ "$(id -u)" -ne 0 ]; then echo "This script must be run as root" >&2 exit 1 fi # Create log directory if it doesn't exist mkdir -p "$(dirname "$LOG_FILE")" limit_log_size "$LOG_FILE" "$MAX_LOG_SIZE_KB" # Verifica o tamanho do log no início # Log script execution start log "Starting GUI Manager Script with action: ${1:-status}" # Determine action based on parameters or environment variable ACTION="" # Check for command line parameter if [ $# -gt 0 ]; then ACTION="$1" fi # Check for environment variable if no parameter provided if [ -z "$ACTION" ] && [ -n "$GUI_ACTION" ]; then ACTION="$GUI_ACTION" fi # Default to status if no action specified if [ -z "$ACTION" ]; then ACTION="status" fi # Execute the appropriate action case "$ACTION" in disable) disable_gui exit $? ;; enable) enable_gui exit $? ;; status) show_gui_status exit $? ;; *) log "Usage: $0 [enable|disable|status]" log "Or: GUI_ACTION=enable|disable $0" log "Invalid action: $ACTION" exit 1 ;; esac