#!/bin/bash # Warn when we have about 15 mins charge left, then every 5 mins # 5 mins ~= 0.1 Wh c5=100000 first_warn=$((${c5} * 3)) next_warn=${first_warn} dir=/sys/class/power_supply/BAT0 snd=/usr/local/share/sounds/BEEPKIND.WAV # We may have been invoked in response to an ACPI event e.g. # "mains off"; it seems that the transitions are not atomic for # example between "mains on; battery charging" and "mains off; # battery discharging" we pass briefly through "mains off; # battery charging". So we'll sleep a bit in the hope that this # resolves itself. sleep 2 while true do if [ ! -d ${dir} ] then # The battery isn't present and presumably we're running on mains # power. Since we have nothing to do, terminate. When the # battery is inserted the ACPI event will cause us to be restarted. exit 0 fi case `cat ${dir}/status` in Discharging) ;; *) # If the battery is not discharging it's probably charging or full, # i.e. the mains is present. In this case we won't bother to monitor # it; when the mains is removed we'll be restarted by the ACPI event. exit 0 ;; esac # We're running on battery power. How much charge do we have? c=`cat ${dir}/charge_now` # If we're reached a warning level, make a warning sound and step to # the next warning level. if [ $c -lt ${next_warn} ] then play ${snd} next_warn=$((next_warn - ${c5})) fi # If we've got lots of charge in the battery, we can sleep # for a long time before checking again. The following # is a guess of how long it would take us to reach the # warning level at a current drain of 3.6 A, which is much # more than we ever actually take. # (This doesn't allow for replacing a good battery with a # bad one.) sleep $(( ( $c - ${next_warn} ) / 1000 )) done