#/bin/bash # battery-monitor.sh # This program lets the user know when the battery needs to be charged. # 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 . # To do. Add code to verify that the user is root, or has run the script with # 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 # a group that is the same as their login name, which work well if you only # want your user to be informed. group="" # Change the battery variable to your batteries identification e.g. BAT0. battery="" 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) # Change the minimum variable to the minimum amount of minutes that a battery # has left before the group is informed to recharge it e.g. 40.00. When I # tested this and the battery-suspend.sh script, 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 about 10 # minutes plus/minus each time that I ran it. Keep the minus 10 minutes in # mind when setting the minimum value. # YOU'VE BEEN WARNED! minimum= if (( $(echo "$minutes < $minimum" | bc -l) )); then echo "Suspending the computer! Battery charge is needed!" | wall -g $group sleep 3 systemctl suspend fi exit 0