2009-12-14
Ah, the age old question "how does one display laptop battery information at the bottom of a screen terminal?"

Well finally, there is an answer. Actually, there are probably quite a few different answers, but this one is my answer. Since I wanted to replicate some of the functionality of my other battery monitor (vattery), this battery monitor can perform commands when the battery level drops to a certain percent. By the way, this requires that acpi is installed and running.

Let's start with the code to read and process the battery info.
#!/bin/sh #we need a function to run when the battery gets low low_battery_action () {     echo "the battery is getting low" } #what files have our needed data?    state_file="/proc/acpi/battery/BAT0/state" info_file="/proc/acpi/battery/BAT0/info" #by default, the action has not been performed action_performed=0 #at what percent does the action get run? action_percent=10 # start the loop while ]; do     #get info about the battery     battery_current_charge=`cat $state_file | grep remaining | awk '{print $3}'`     battery_ac_state=`cat $state_file | grep charging | awk '{print $3}'`     battery_last_total_charge=`cat $info_file | grep last | awk '{print $4}'`     battery_percent=$((100*battery_current_charge/battery_last_total_charge))     #display the info     echo "battery: $battery_percent% $battery_ac_state"     if $action_performed -eq # has the low power action been performed?     then    # if "yes"         if $battery_ac_state "charging" # is the battery charging?         then #if "yes"             #is the battery percent greater than the action percent?             if $battery_percent -gt $action_percent ]             then                 #reset the action_performed to false                 action_performed=0             fi         fi     else #the low power action has not been performed         #is the battery percent less than or equal to the action percent?         if $battery_percent -le $action_percent ]         then              #perform the action             low_battery_action             #set the action_performed to true             action_performed=1         fi     fi     # take a two second nap     sleep 2 done
I have this saved as battery_state.sh in my ~/bin directory, which is in my $PATH. If ~/bin isn't part of your $PATH, don't worry, you can just make a directory called "scripts" and put the file in the "scripts" directory. The file should be set to executable by issuing
chmod +x ~/bin/battery_state.sh
Adjust the command accordingly if your file is not in the ~/bin directory.

Since the code doesn't do anything major when the battery charge drops to 10 percent, change line 5 to whatever command or commands you wish to occur. I'm a big fan of
espeak -g7 -ven-us+f3 "Hello? is anyone there? my battery power is low. please plug in my charger."
To adjust when the command is run, edit line 13 and set the action_percent to the percent of charge that the battery needs to reach for the command to run.

Now it is time to setup screen to run the battery_state.sh script and process the scripts output when screen is run. To do this, the ~/.screenrc file needs to be edited (if the file doesn't exist, just create the file).

The following should be added to the .screenrc file
hardstatus alwayslastline backtick 1 0 0 battery_state.sh hardstatus string '%1`'
Now start screen.
If the battery_state.sh file is not in a directory that is part of your $PATH, adjust the backtick 1 0 0 battery_state.sh line to be backtick 1 0 0 /path/to/your/battery_state.sh using the actual path to your battery_state.sh file.

Now quit reading, and go write something.
Comments
2010-03-21 jamie trenchard:
Works perfectly for me. (Except I had to change the path to my battery, but hey, at least it's not like the net going down)
Now I don't have to worry about my laptop suddenly running out of battery.
2010-03-21 jezra:
Quite a few distros these days are starting to count batteries beginning with 1 instead of 0. So instead of information being in "/proc/acpi/battery/BAT0" it might be in "/proc/acpi/battery/BAT1". Similarly, this code doesn't handle multiple batteries.
Name:
not required
Email:
not required (will not be displayed)
Website:
not required (will link your name to your site)
Comment:
required
Please do not post HTML code or bbcode unless you want it to show up as code in your post. (or if you are a blog spammer, in which case, you probably aren't reading this anyway).
Prove you are human by solving a math problem! I'm sorry, but due to an increase of blog spam, I've had to implement a CAPTCHA.
Problem:
1 plus 6
Answer:
required
  • Tags:
subscribe
 
2019
2016
2015
2014
2013
2012
2011
2010
December
November
October
September
August
July
June
May
April
March
February
January
2009
December
November
October
September
August
July
June
May
April
March
February
January
2008