debian-scripts/battery-monitor.sh

77 lines
2.7 KiB
Bash
Raw Normal View History

2024-10-21 19:18:55 -04:00
#/bin/bash
# battery-monitor.sh
2024-11-25 14:38:57 -05:00
2024-11-25 15:14:09 -05:00
# Once a user defined minimum is reached, this program lets the user know
# when the battery needs to be charged and suspends the computer.
# This program has been verified to work on a Debian 12 GNU/Linux x86_64
# default console installation, but the installation of the bc program is
# required.
2024-11-25 14:38:57 -05:00
2024-10-23 20:56:09 -04:00
# Copyright (c) 2024, Scott C. MacCallum (scott@scm-guru.live).
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2024-11-25 14:38:57 -05:00
# To do. Add code to verify that the user is root or has run the script with
2024-10-23 20:56:09 -04:00
# sudo. Add code to verify that the bc program is installed.
# Change this variable to the group that should be informed of a need to
# charge the battery. On GNU/Linux distributions users are often part of
2024-10-24 07:35:19 -04:00
# a group that is the same as their login name, which works well if you only
2024-11-25 15:14:09 -05:00
# want your user to be informed on the console.
2024-11-25 14:38:57 -05:00
group="scm"
2024-10-23 20:56:09 -04:00
2024-11-25 14:38:57 -05:00
# Change the battery variable to your batteries identification.
2024-10-23 20:56:09 -04:00
2024-11-25 14:38:57 -05:00
battery="BAT0"
2024-10-21 19:18:55 -04:00
2024-11-25 14:38:57 -05:00
# If the battery is fully charged.
if (( minutes == 0 )); then
exit 0
fi
2024-10-23 20:56:09 -04:00
2024-11-25 15:14:09 -05:00
# Calculate the state of the battery.
2024-10-21 19:18:55 -04:00
charge=$(cat /sys/class/power_supply/$battery/charge_now)
discharge=$(cat /sys/class/power_supply/$battery/current_now)
hours=$(echo "scale=2; $charge / $discharge" | bc -l)
minutes=$(echo "scale=2; $hours * 60" | bc -l)
2024-10-23 20:56:09 -04:00
# Change the minimum variable to the minimum amount of minutes that a battery
2024-11-25 14:38:57 -05:00
# is estimated to have left before the group is informed to recharge it. When
2024-11-25 15:14:09 -05:00
# I tested this, I was surprised to discover that the computer turned off
# despite having reported that there was 10 minutes left! I created the
# battery-status.sh script to aid in my understanding of what was going on,
# and I discovered a variance of minutes plus/minus each time that I ran it,
# so the system is clearly making an estimate. Keep this in mind when setting
# the minimum value.
2024-11-25 14:38:57 -05:00
# Change this variable to you liking, but less than 40.00 is likely a bad
# idea. YOU'VE BEEN WARNED!
2024-10-23 20:56:09 -04:00
2024-11-25 14:38:57 -05:00
minimum=40.00
2024-10-23 20:56:09 -04:00
2024-11-25 15:14:09 -05:00
If the minutes of charge are less than the minimum.
2024-10-21 19:18:55 -04:00
if (( $(echo "$minutes < $minimum" | bc -l) )); then
echo "Suspending the computer! Battery charge is needed!" | wall -g $group
2024-10-23 20:56:09 -04:00
sleep 3
systemctl suspend
2024-10-21 19:18:55 -04:00
fi
exit 0