You are not logged in.

#1 2012-06-06 08:10:44

Lockheed
Member
Registered: 2010-03-16
Posts: 1,542

Missing battery files in /proc/acpi/battery

I'm trying to employ this script:
http://mindspill.net/computing/linux-no … y-warning/
to get low battery warnings. It draws info from files like
BAT_STATE="/proc/acpi/battery/BAT1/state"
BAT_INFO="/proc/acpi/battery/BAT1/info"
but folder /proc/acpi/battery/ does not exist in my system. I do have /sys/class/power_supply/BAT0/ folder, but it does not containt 'state' or 'info' files.

What should I do to make this work?

Last edited by Lockheed (2012-06-06 10:49:13)

Offline

#2 2012-06-06 08:24:06

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Missing battery files in /proc/acpi/battery

What have you tried?


Have you attemped to use the correlates to those that are in /sys/class/power_supply/BAT0...


Also: moving to Programming and Scripting as you really seem to be asking for some help to rewrite that script.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2012-06-06 08:55:18

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Missing battery files in /proc/acpi/battery

I have the same situation re the files. The ones you need are all in the /sys/class/power_supply/XXX directory (where XXX refers to your battery - it does vary).
status: tells you if it's charging, etc.
charge_full: tells you capacity
charge_now: tells you current value.

You can drop the awk calls from that script (and the needless grep calls) and just take the contents of those files for your calculation.

On the other hand, these days I just call acpi and cut the output.


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#4 2012-06-06 08:58:02

Lockheed
Member
Registered: 2010-03-16
Posts: 1,542

Re: Missing battery files in /proc/acpi/battery

I would rather operate on time remaining in minutes or %, than voltage values. My conky displays it in % and minutes. Can this be used in this script?

Offline

#5 2012-06-06 09:11:19

skunktrader
Member
From: Brisbane, Australia
Registered: 2010-02-14
Posts: 1,584

Re: Missing battery files in /proc/acpi/battery

Look at the output of acpi -V to see if the info you prefer is displayed and work from there

Offline

#6 2012-06-06 09:15:32

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Missing battery files in /proc/acpi/battery

Sorry, I've just noticed the "advanced" script further down the page.

BAT_REMAIN can be obtained from /sys/class/power_supply/BAT0/current_charge.
BAT_DISCHARGE will need to be calculated. Get the previous BAT_REMAIN value, subtract the current one and divide by the number of seconds between the reading.
BAT_WARN I think can be obtained from /sys/class/power_supply/BAT0/alarm
BAT_LOW I don't know where you get that from, unless it's the alarm value.

EDIT: but yeah, you could just get acpi output or acpi -V. That's what I do.

Last edited by skanky (2012-06-06 09:16:50)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#7 2012-06-06 10:02:19

Lockheed
Member
Registered: 2010-03-16
Posts: 1,542

Re: Missing battery files in /proc/acpi/battery

Ok, hmm... So what am I supposed to do with acpi -V  output?

Offline

#8 2012-06-06 10:12:01

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Missing battery files in /proc/acpi/battery

Well, for example, this gets me the percentage:

$acpi | awk '{print $4}'
98%

"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#9 2012-06-06 10:21:17

Lockheed
Member
Registered: 2010-03-16
Posts: 1,542

Re: Missing battery files in /proc/acpi/battery

Maybe I am being a bit thick, but am not well versed in scripts.
I already modified the script to use notify-send rather than zenity

#!/bin/bash

# Location of acpi files.
BAT_STATE="/proc/acpi/battery/BAT1/state"
BAT_INFO="/proc/acpi/battery/BAT1/info"

# Minutes from the in-built warning level that measures should be taken to prevent loosing data.
BAT_ALERT_TIME=15

# Log to syslog & /var/log/acpid 
LOGGER="/usr/bin/logger -t ACPID"

if grep -q discharging $BAT_STATE
then
   BAT_REMAIN=`awk '/remaining/ { print $3 }' $BAT_STATE` # Remaining mAh.
   BAT_DISCHARGE=`awk '/rate/ { print $3 }' $BAT_STATE` # Discharge rate in mA.
   BAT_WARN=`awk '/warning/ { print $4 }' $BAT_INFO` # Warning level in mAh.
   BAT_LOW=`awk '/low/ { print $4 }' $BAT_INFO` # Low level in mAh.

   BAT_ALERT=$(( ( ( $BAT_DISCHARGE / 60 ) * $BAT_ALERT_TIME ) )) #+ $BAT_WARN  )) # Work out the charge that will have been used in $BAT_ALERT_TIME minues (and add the warning level charge as a buffer zone).

   #echo "BAT_REMAIN = $BAT_REMAIN"
   #echo "BAT_ALERT = $BAT_ALERT"

   # These environment variables are needed so that the script can post to the current display (using the zenity command). Change mention of steph to whatever your username is.
   export XAUTHORITY=/home/steph/.Xauthority
   export DISPLAY=:0

   if 
   then
       # Log the fact that the (critically) low level has been reached.
       $LOGGER -p daemon.warn "Battery capacity is critically low, shutting down the system."

       # Pop up critical warning.
		/usr/bin/notify-send "Battery critically low." "HIBERNATING ($BAT_REMAIN)" &
#       zenity --warning --title "Battery Critically Low" --text "Battery capacity is critically low, shutting down the system. (remain = $BAT_REMAIN)" &
       
       # This is probably a good place to shut down the computer to make sure all data is written to disk. It may even be too late by now, in which case shut down the computer when it reaches the in-built warning level.
       dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Hibernate
       
   elif 
   then
       # Log the fact that the warning level has been reached.
       $LOGGER -p daemon.warn "Battery capacity is low."

       # Pop up warning.
       /usr/bin/notify-send "Battery low." "(remain = $BAT_REMAIN)" &
#       zenity --warning --title "Battery Low" --text "Battery capacity is low. (remain = $BAT_REMAIN)" &
              
   elif 
   then
       # Log this to let the system know that there's only a few minutes left of battery before it hit the warning level of the battery pack
       $LOGGER -p daemon.info "Battery capacity alert."

       # Pop up info.
		/usr/bin/notify-send "Battery depleting." "(remain = $BAT_REMAIN)" &
#       zenity --info --title "Battery Alert" --text "Battery capacity alert. (remain = $BAT_REMAIN)" &
       
   fi

fi

However, I suppose changing it to use acpi -V would involve more in-depth modifications which I am not sure of.

Offline

#10 2012-06-06 10:44:38

mloskot
Member
From: London, United Kingdom
Registered: 2012-06-01
Posts: 86
Website

Re: Missing battery files in /proc/acpi/battery

Lockheed wrote:

but folder /proc/acpi/battery/ does not exist in my system.

Yes, because power directories in /proc/acpi have been deprecated.

Lockheed wrote:

What should I do to make this work?

Use more up to date power monitor scripts, like batterymon-clone (also in AUR)
or fix the legacy scripts to use the new  /sys/class/power_supply functions - may be challenging.

p.s. For sake of clarity and easy searching, I'd suggest to change the subject line of this thread to: Missing battery files in /proc/acpi/battery

Last edited by mloskot (2012-06-06 10:45:55)


Mateusz Loskot | github | archlinux-config
Arch (x86-64) | ThinkPad T400  | Intel P8600| Intel i915
Arch (x86-64) | ThinkPad W700 | Intel T9600 | NVIDIA Quadro FX 2700M

Offline

#11 2012-06-06 10:55:54

Lockheed
Member
Registered: 2010-03-16
Posts: 1,542

Re: Missing battery files in /proc/acpi/battery

That looks good, but I don't need another tray icon.

Offline

#12 2012-06-06 10:59:39

mloskot
Member
From: London, United Kingdom
Registered: 2012-06-01
Posts: 86
Website

Re: Missing battery files in /proc/acpi/battery

Lockheed wrote:

That looks good, but I don't need another tray icon.

Check source of the batterymon
Look how the /sys/class/power_supply/* files are read and used to calculate the human-friendly battery status info.
Replicate the steps in Shell script and you are done.


Mateusz Loskot | github | archlinux-config
Arch (x86-64) | ThinkPad T400  | Intel P8600| Intel i915
Arch (x86-64) | ThinkPad W700 | Intel T9600 | NVIDIA Quadro FX 2700M

Offline

#13 2012-06-06 11:04:41

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Missing battery files in /proc/acpi/battery

EDIT: cross-posted, apologies.

Untested, so there may be a mistake or two in there:

cur=$(acpi | awk '{gsub("\%", "", $4);print $4}')
if (( $cur < 20 )); then

elif (( $cur < 30 )); then

else (( $cur < 50 )); then

fi

The bits inside the if statements are as you want, and you may want to change the thresholds.
This will also trigger the alerts every time the script runs - so if you have pop-ups you may want to set up a flag to show that you've already shown the pop-up. The main script may do that, I didn't read the code in detail.

Last edited by skanky (2012-06-06 11:05:21)


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#14 2012-06-06 18:55:56

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Missing battery files in /proc/acpi/battery

skanky wrote:

acpi | awk '{gsub("\%", "", $4);print $4}'

You can also fudge it with:

awk '{print +$4}' <(acpi)

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#15 2012-06-07 09:02:13

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Missing battery files in /proc/acpi/battery

jasonwryan wrote:
skanky wrote:

acpi | awk '{gsub("\%", "", $4);print $4}'

You can also fudge it with:

awk '{print +$4}' <(acpi)

That + is new to me, thanks. I'll have to have another look at the man page again. smile


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

Board footer

Powered by FluxBB