diff options
| author | Craig Jennings <c@cjennings.net> | 2025-05-21 22:01:35 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2025-05-21 22:01:35 -0500 |
| commit | b4463015b97912658d630377fafbf630f7588d1e (patch) | |
| tree | d04b66d992fe2ce88391889c21c5d8dc97acd0ef /dotfiles/system/.local/bin/battery_monitor | |
| parent | 548154ea395356868e87980b149dfc0abdc84e17 (diff) | |
moving arch dotfiles into archsetup
Diffstat (limited to 'dotfiles/system/.local/bin/battery_monitor')
| -rwxr-xr-x | dotfiles/system/.local/bin/battery_monitor | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/dotfiles/system/.local/bin/battery_monitor b/dotfiles/system/.local/bin/battery_monitor new file mode 100755 index 0000000..7c6e013 --- /dev/null +++ b/dotfiles/system/.local/bin/battery_monitor @@ -0,0 +1,52 @@ +#!/usr/bin/env bash +# battery_monitor +# Intended to be run via .xinitrc +# - Exit automatically if no battery (desktop) +# - When below 15%, warn user of low battery +# - When below 10%, suspend within 10 seconds if not charging +# +# Craig Jennings <c@cjennings.net> + +# check if acpi is installed +if ! command -v acpi &> /dev/null; then + echo "acpi is not installed. Cannot continue. Exiting...." + exit 1 +fi + +# exit if a battery exists +if [ ! -d "/sys/class/power_supply/BAT0" ] && [ ! -d "/sys/class/power_supply/BAT1" ]; then + echo "Acpi is installed but no battery detected. Assuming this is a desktop and exiting...." + exit 1 +fi + +while true; do + # Get the current battery percentage using acpi + battery_percentage=$(acpi -b | awk -F ', ' '{print $2}' | tr -d '%') + # battery_percentage=$(acpi -b | awk -F ', ' '{print $2}' | sed 's/%//') + + # When below 10%, suspend within 10 seconds if not charging + if [ "$battery_percentage" -lt 11 ] && ! acpi -a | grep -q "on-line" ; then + # Send a notification of sleeping in 10 seconds + notify-send "Critical Battery" "Battery is at $battery_percentage%. System entering sleep in 30 seconds." + + # sleep for 10 seconds, then abort if charging + sleep 30 + + # Check if the system is charging (AC adapter connected) + if acpi -a | grep -q "on-line"; then + notify-send "Charging" "The system is now charging. No action taken." + else + notify-send "Critical Battery" "Putting the system to sleep." + sudo systemctl suspend + fi + fi + + # When below 15%, warn user + if [ "$battery_percentage" -lt 15 ] && ! acpi -a | grep -q "on-line" ; then + # Send a notification using notify-send and dunst + notify-send "Low Battery" "Battery is at $battery_percentage%. System will automatically sleep at 10%." + fi + + # Sleep for 5 minutes before checking again + sleep 300 +done |
