#!/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" # --- Functions --- # 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 echo "System doesn't use systemd" fi } # Function to show GUI status show_gui_status() { if ! is_systemd; then echo "$(date): System doesn't use systemd" | tee -a "$LOG_FILE" return 1 fi current_target=$(get_current_target) dm=$(get_display_manager) echo "$(date): Current system target: $current_target" | tee -a "$LOG_FILE" echo "$(date): Display Manager: $(basename "$dm")" | tee -a "$LOG_FILE" if [ "$current_target" = "graphical.target" ]; then echo "$(date): Graphical environment is ENABLED" | tee -a "$LOG_FILE" else echo "$(date): Graphical environment is DISABLED" | tee -a "$LOG_FILE" fi # Show memory usage echo "$(date): Memory usage:" | tee -a "$LOG_FILE" free -h | awk '/^Mem:/ {print " Total: " $2 ", Used: " $3 ", Free: " $4}' | tee -a "$LOG_FILE" } # Function to disable graphical environment disable_gui() { if ! is_systemd; then echo "$(date): Error: This system doesn't use systemd" | tee -a "$LOG_FILE" return 1 fi current_target=$(get_current_target) if [ "$current_target" != "graphical.target" ]; then echo "$(date): Graphical environment is already disabled" | tee -a "$LOG_FILE" return 0 fi echo "$(date): Disabling graphical environment..." | tee -a "$LOG_FILE" # Get current display manager dm=$(get_display_manager) if [ "$dm" = "unknown" ]; then echo "$(date): Error: Could not determine display manager" | tee -a "$LOG_FILE" return 1 fi # Show memory usage before echo "$(date): Memory usage before disabling GUI:" | tee -a "$LOG_FILE" free -h | awk '/^Mem:/ {print " Total: " $2 ", Used: " $3 ", Free: " $4}' | tee -a "$LOG_FILE" # Stop display manager echo "$(date): Stopping display manager $(basename "$dm")..." | tee -a "$LOG_FILE" systemctl stop "$(basename "$dm")" # Disable graphical target echo "$(date): Setting default target to multi-user.target..." | tee -a "$LOG_FILE" systemctl set-default multi-user.target # Show memory usage after echo "$(date): Memory usage after disabling GUI:" | tee -a "$LOG_FILE" free -h | awk '/^Mem:/ {print " Total: " $2 ", Used: " $3 ", Free: " $4}' | tee -a "$LOG_FILE" echo "$(date): Graphical environment disabled successfully" | tee -a "$LOG_FILE" return 0 } # Function to enable graphical environment enable_gui() { if ! is_systemd; then echo "$(date): Error: This system doesn't use systemd" | tee -a "$LOG_FILE" return 1 fi current_target=$(get_current_target) if [ "$current_target" = "graphical.target" ]; then echo "$(date): Graphical environment is already enabled" | tee -a "$LOG_FILE" return 0 fi echo "$(date): Enabling graphical environment..." | tee -a "$LOG_FILE" # Get current display manager dm=$(get_display_manager) if [ "$dm" = "unknown" ]; then echo "$(date): Error: Could not determine display manager" | tee -a "$LOG_FILE" return 1 fi # Enable graphical target echo "$(date): Setting default target to graphical.target..." | tee -a "$LOG_FILE" systemctl set-default graphical.target # Start display manager echo "$(date): Starting display manager $(basename "$dm")..." | tee -a "$LOG_FILE" systemctl start "$(basename "$dm")" echo "$(date): Graphical environment enabled successfully" | tee -a "$LOG_FILE" return 0 } # --- Main Execution --- # Create log directory if it doesn't exist mkdir -p /var/log # 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 ;; enable) enable_gui ;; status) show_gui_status ;; *) echo "Usage: $0 [enable|disable|status]" echo "Or: GUI_ACTION=enable|disable $0" echo "Invalid action: $ACTION" exit 1 ;; esac