#!/bin/bash # Find all battery devices (BAT0, BAT1, etc.) BATTERY_DEVICES=$(upower -e | grep 'BAT') ON_BATTERY=false # Iterate through all detected batteries for DEVICE in $BATTERY_DEVICES; do # Get the state for the current battery device STATE=$(upower -i $DEVICE | grep "state" | awk '{print $2}') # Check if the state is 'discharging' for ANY battery if [ "$STATE" = "discharging" ]; then ON_BATTERY=true break # Exit the loop as soon as one is found to be discharging fi done # Check the final condition and suspend if true if $ON_BATTERY; then systemctl suspend fi