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.
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
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
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.
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.
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#!/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 [ 1 ]; 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 1 ] # 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
Adjust the command accordingly if your file is not in the ~/bin directory.chmod +x ~/bin/battery_state.sh
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
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.espeak -g7 -ven-us+f3 "Hello? is anyone there? my battery power is low. please plug in my charger."
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
Now start screen.hardstatus alwayslastline backtick 1 0 0 battery_state.sh hardstatus string '%1`'
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.
Now I don't have to worry about my laptop suddenly running out of battery.