#!/bin/bash # TLP Status and Configuration Tool # Displays TLP status and provides configuration management set -euo pipefail # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Function to show usage show_usage() { echo "TLP Status and Configuration Tool" echo echo "Usage: $0 [OPTIONS]" echo echo "Options:" echo " -s, --status Show TLP status (default)" echo " -c, --config Show TLP configuration" echo " -r, --recommendations Show optimization recommendations" echo " -t, --toggle-mode Toggle between AC/BAT mode" echo " -b, --battery-info Show detailed battery information" echo " -p, --profile PROFILE Apply power profile (powersave|balanced|performance)" echo " -h, --help Show this help message" echo echo "Examples:" echo " $0 Show TLP status" echo " $0 -c Show configuration" echo " $0 -p powersave Apply power saving profile" echo " $0 -r Show recommendations" } # Function to check if TLP is installed check_tlp() { if ! command -v tlp &> /dev/null; then echo -e "${RED}Error: TLP is not installed${NC}" echo "Please run the installation script first: ./scripts/install.sh" exit 1 fi } # Function to show TLP status show_status() { echo -e "${CYAN}TLP Status Information:${NC}" echo "========================================" echo # Basic TLP status echo -e "${BLUE}System Status:${NC}" tlp-stat -s echo # Power source echo -e "${BLUE}Power Source:${NC}" tlp-stat | grep -A 5 "Power source" echo # TLP service status echo -e "${BLUE}TLP Service:${NC}" systemctl is-active tlp.service && echo -e "Status: ${GREEN}Active${NC}" || echo -e "Status: ${RED}Inactive${NC}" systemctl is-enabled tlp.service && echo -e "Enabled: ${GREEN}Yes${NC}" || echo -e "Enabled: ${RED}No${NC}" echo # Current power profile echo -e "${BLUE}Current Power Profile:${NC}" tlp-stat | grep -E "Mode|operation mode" | head -3 echo } # Function to show TLP configuration show_config() { echo -e "${CYAN}TLP Configuration:${NC}" echo "========================================" echo # CPU settings echo -e "${BLUE}CPU Settings:${NC}" tlp-stat -p echo # Disk settings echo -e "${BLUE}Disk Settings:${NC}" tlp-stat -d echo # Graphics settings echo -e "${BLUE}Graphics Settings:${NC}" tlp-stat -g echo # USB settings echo -e "${BLUE}USB Settings:${NC}" tlp-stat -u echo } # Function to show battery information show_battery_info() { echo -e "${CYAN}Battery Information:${NC}" echo "========================================" echo tlp-stat -b echo # Additional battery health info if [[ -d "/sys/class/power_supply/BAT0" ]]; then echo -e "${BLUE}Battery Health Details:${NC}" local cycle_count=$(cat /sys/class/power_supply/BAT0/cycle_count 2>/dev/null || echo "N/A") local manufacture_date=$(cat /sys/class/power_supply/BAT0/manufacture_date 2>/dev/null || echo "N/A") local serial=$(cat /sys/class/power_supply/BAT0/serial_number 2>/dev/null || echo "N/A") echo "Cycle count: $cycle_count" echo "Manufacture date: $manufacture_date" echo "Serial number: $serial" echo fi } # Function to show recommendations show_recommendations() { echo -e "${CYAN}Power Optimization Recommendations:${NC}" echo "========================================" echo # Check current settings and provide recommendations local current_governor=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null || echo "N/A") local current_brightness=$(cat /sys/class/backlight/*/brightness 2>/dev/null | head -1 || echo "N/A") local max_brightness=$(cat /sys/class/backlight/*/max_brightness 2>/dev/null | head -1 || echo "1") echo -e "${BLUE}Current System State:${NC}" echo "CPU Governor: $current_governor" if [[ "$current_brightness" != "N/A" && "$max_brightness" != "1" ]]; then local brightness_percent=$(( current_brightness * 100 / max_brightness )) echo "Screen brightness: ${brightness_percent}%" if [[ $brightness_percent -gt 70 ]]; then echo -e "${YELLOW}• Consider reducing screen brightness to save power${NC}" fi fi echo echo -e "${BLUE}Recommendations:${NC}" # Check if on battery local power_source=$(tlp-stat -s | grep "Power source" | awk '{print $3}' || echo "unknown") if [[ "$power_source" == "battery" ]]; then echo -e "${GREEN}✓ Running on battery - power saving mode active${NC}" # Check governor if [[ "$current_governor" != "powersave" ]]; then echo -e "${YELLOW}• Consider switching to 'powersave' CPU governor${NC}" fi # Check for running services echo -e "${YELLOW}• Close unnecessary applications${NC}" echo -e "${YELLOW}• Disable unused wireless interfaces${NC}" echo -e "${YELLOW}• Enable laptop mode if available${NC}" else echo -e "${BLUE}Running on AC power${NC}" echo -e "• Power saving less critical, but still beneficial" fi # Check for power-hungry processes echo echo -e "${BLUE}Power-hungry processes (top CPU users):${NC}" ps aux --sort=-%cpu | head -6 | awk 'NR>1{printf "%-20s %s%%\n", $11, $3}' echo echo -e "${BLUE}Quick optimization commands:${NC}" echo "• sudo tlp bat # Force battery mode" echo "• sudo tlp ac # Force AC mode" echo "• sudo powertop --auto-tune # Apply PowerTOP optimizations" echo } # Function to toggle power mode toggle_mode() { local current_mode=$(tlp-stat -s | grep "Mode" | awk '{print $2}' | tr -d '[]') echo -e "${CYAN}Current mode: $current_mode${NC}" if [[ "$current_mode" == "AC" ]]; then echo "Switching to battery mode..." sudo tlp bat echo -e "${GREEN}Switched to battery mode${NC}" else echo "Switching to AC mode..." sudo tlp ac echo -e "${GREEN}Switched to AC mode${NC}" fi } # Function to apply power profile apply_profile() { local profile=$1 case $profile in "powersave") echo -e "${CYAN}Applying power saving profile...${NC}" sudo tlp bat # Additional power saving commands echo 'powersave' | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor > /dev/null echo -e "${GREEN}Power saving profile applied${NC}" ;; "balanced") echo -e "${CYAN}Applying balanced profile...${NC}" sudo tlp start echo 'ondemand' | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor > /dev/null 2>&1 || \ echo 'schedutil' | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor > /dev/null echo -e "${GREEN}Balanced profile applied${NC}" ;; "performance") echo -e "${CYAN}Applying performance profile...${NC}" sudo tlp ac echo 'performance' | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor > /dev/null echo -e "${GREEN}Performance profile applied${NC}" echo -e "${YELLOW}Note: This will increase power consumption${NC}" ;; *) echo -e "${RED}Invalid profile: $profile${NC}" echo "Valid profiles: powersave, balanced, performance" exit 1 ;; esac } # Main function main() { local action="status" local profile="" # Parse command line arguments while [[ $# -gt 0 ]]; do case $1 in -s|--status) action="status" shift ;; -c|--config) action="config" shift ;; -r|--recommendations) action="recommendations" shift ;; -t|--toggle-mode) action="toggle" shift ;; -b|--battery-info) action="battery" shift ;; -p|--profile) action="profile" profile="$2" shift 2 ;; -h|--help) show_usage exit 0 ;; *) echo "Unknown option: $1" show_usage exit 1 ;; esac done # Check if TLP is installed check_tlp # Execute requested action case $action in "status") show_status ;; "config") show_config ;; "recommendations") show_recommendations ;; "toggle") toggle_mode ;; "battery") show_battery_info ;; "profile") if [[ -z "$profile" ]]; then echo -e "${RED}Error: Profile name required${NC}" show_usage exit 1 fi apply_profile "$profile" ;; esac } # Run main function if script is executed directly if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then main "$@" fi