You are not logged in.
Pages: 1
Hello!
I hope you could help me out, as I am stuck.
I am trying to automate pm-utils stuff on my laptop, that I have until now been doing by hand with CLI. I installed acpid and I really like how it works as it is very easy and clean to add event handling for closing laptop lid etc.
But I was unable to use /etc/acpi/handler.sh to detect low battery warning. Only event that is received under "battery" is when the AC adapter is plugged in or battery is removed/inserted, but not when battery is low.
I tried to find more info on this, but discovered only that it is supposedly possible to configure alarm level in /proc/acpi/battery/BAT1/alarm, but that file seams to be depreciated and it's replacement (?) /sys/class/power_supply/BAT1/alarm contains a number resulting in about 3% charge, that should have been sufficient for at least writing to log file if any event was received from the battery, but nothing was. (And anyway, I don't know how to configure it using this file, as I am not allowed to write to it and it gets overwriten by something.)
So I could really use some pointers on why the acpi event for low battery doesn't get generated (or why I am missing it) or what alternatives I have.
I really would not like to run a cron job for checking battery level, as the acpi event method seams much more beautiful to me and would allow me to sleep better.
Thanks a lot!
Last edited by beatle (2012-01-06 01:20:29)
Offline
I nice thing to have installed when having a laptop system is laptop-mode-tools. You can enable it when on both AC and BAT, or just BAT. latop-mode-tools uses /etc/acpi/events/lm_battery and /etc/acpi/actions/lm_battery.sh to start/stop on low battery.
Last edited by hesse (2012-01-05 17:01:53)
Offline
I will check it out. Thanks!
Offline
You might find the scripts useful that I put on page 67 of this thread. It does rely on the command,
acpi -b
giving meaningful output.
Offline
You might find the scripts useful that I put on page 67 of this thread. It does rely on the command,
acpi -b
giving meaningful output.
This is great! Marking as solved, as this solution will let me sleep.
Thanks hesse and dev/zero for your help!
Offline
No worries :-) If you can think of any ways to improve the script, I'd be interested to know. It isn't perfect because output from "acpi -b" is unpredictable.
Offline
No worries :-) If you can think of any ways to improve the script, I'd be interested to know. It isn't perfect because output from "acpi -b" is unpredictable.
I did adapt your script a bit to suite my needs (Thanks for showing how to write deamon scripts! I haven't thought that it is so straightforward and beauty full.) , but I wouldn't say I improved it as I am basically illiterate in bash.
For now I deal with the output of "acbi -b" by searching for the "NN%" (with grep, as I know not of a better way) - remaining percent of battery charge - in it, as I suspect this sub string would allays appear in "acbi -b" output even when the 'remaining time' part is unpredictable (haven't checked it though!).
Also, my laptop is usually on mute so I changed the text-to-speech warning to a i3-nagbar popping up. (And along the way learned how to control spawned processes in bash! I love just GNU/Linux!).
/usr/sbin/battery_monitor_loop
#! /bin/bash
SLEEP_TIME=5 # Default time between checks.
SAFE_PERCENT=30 # Still safe at this level.
DANGER_PERCENT=15 # Warn when battery at this level.
CRITICAL_PERCENT=5 # Hibernate when battery at this level.
NAGBAR_PID=0
export DISPLAY=:0.0
function launchNagBar
{
i3-nagbar -m 'Battery low!' -b 'Hibernate!' 'pm-hibernate' >/dev/null 2>&1 &
NAGBAR_PID=$!
}
function killNagBar
{
if [[ $NAGBAR_PID -ne 0 ]]; then
ps -p $NAGBAR_PID | grep "i3-nagbar"
if [[ $? -eq 0 ]]; then
kill $NAGBAR_PID
fi
NAGBAR_PID=0
fi
}
while [ true ]; do
killNagBar
if [[ -n $(acpi -b | grep -i discharging) ]]; then
rem_bat=$(acpi -b | grep -Eo "[0-9]+%" | grep -Eo "[0-9]+")
if [[ $rem_bat -gt $SAFE_PERCENT ]]; then
SLEEP_TIME=10
else
SLEEP_TIME=5
if [[ $rem_bat -le $DANGER_PERCENT ]]; then
SLEEP_TIME=2
launchNagBar
fi
if [[ $rem_bat -le $CRITICAL_PERCENT ]]; then
SLEEP_TIME=1
pm-hibernate
fi
fi
else
SLEEP_TIME=10
fi
sleep ${SLEEP_TIME}m
done
Hm, and just now I got an idea, that, in order to avoid the unpredictable "acpi -b" I could use the acpi information form /sys/class/power_supply/BAT1/* directly as I already have a deamon running. Will try that out.
Last edited by beatle (2012-01-06 18:17:41)
Offline
Nice :-)
I like your idea of sourcing information from
/sys/class/power_supply/BAT?/
I got my stfw on and found Battery information in sysfs for userspace tools. I have:
$ find /sys/class/power_supply/BAT0/ -type f -regex ".*\_now" -exec basename {} \;
voltage_now
current_now
charge_now
So, using the formula I will test whether it works to replace
rem_time=$(acpi -b | awk '{print $(NF-1)}')
hrs=$(date --date="$rem_time" +%H)
mins=$(date --date="$rem_time" +%M)
rem_mins=$(echo "$hrs*60 + $mins - $BUFFER_TIME" | bc)
with something like
BAT_INFO_DIR=/sys/class/power_supply/BAT0
CHARGE_NOW=$(cat $BAT_INFO_DIR/charge_now)
CURR_NOW=$(cat $BAT_INFO_DIR/current_now)
rem_mins=$(echo "60*$CHARGE_NOW/$CURRENT_NOW" | bc)
Offline
Pages: 1