27681 total geeks with 3539 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 



User's box

Forgot password?
New account

[b][url=http ://www.links oflondon-lon don.com/]lin ks of london<stron g><a href="http:/ /www.linksof london-londo n.com/">link s of london outlet store</a></s trong> <br> <strong><a href="http:/ /www.linksof london-londo n.com/">Chea p Links Of London Jewelry Wholesal
[b]<a href="http:/ /www.shzwbg. com/">swiss repl<strong> <a href="http:/ /www.shzwbg. com/">swiss replica watches aaa+</a></st rong> <br> <strong><a href="http:/ /www.shzwbg. com/">swiss replica watches</a>< /strong> <br >
[b][url=http ://www.ippwa tch.com/]swi ss replic<stron g><a href="http:/ /www.ippwatc h.com/">swis s replica watches aaa+</a></st rong> <br> <strong><a href="http:/ /www.ippwatc h.com/">swis s replica watches</a>< /strong> <br >
[b][url=http ://replicawa tches.nboutl etshoes.com/ ]swiss re<strong><a href="http:/ /replicawatc hes.nboutlet shoes.com/"> swiss replica watches aaa+</a></st rong> <br> <strong><a href="http:/ /replicawatc hes.nboutlet shoes.com/"> swiss replica watches</a>< /strong>
[b][url=http ://www.rolex menwatchesco py.com/]role x<strong><a href="http:/ /www.rolexme nwatchescopy .com/">rolex Yacht-Master II</a></stro ng> <br> <strong><a href="http:/ /www.rolexme nwatchescopy .com/">repli ca watches</a>< /strong> <br >

Donate and help us fund new challenges
Due Date: Oct 31
October Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00

News Feeds
The Register
HP Enterprise CEO
Meg Whitman slams
EMC/Dell deal
Netgear prodded
into patching
broadband router
Faked NatWest,
Halifax bank sites
score REAL security
Volvo to "accept
full liability" for
crashes with its
driverless cars
Which data centre
network topology"s
best? Depends on
what you want to
Hackers can steal
SYNful Knock is no
Stuxnet, says
NASA preps test of
aaace project
by KickStarter ban
Oi, Google! Your
AdMob is letting
smut-slingers drop
malvertising into
Android apps!
First Legal Union
of Illegal Street
Vendors Created In
Australian ISPs Not
Ready For Mandatory
Data Retention
NASA Chief Says Ban
On Chinese
Partnerships Is
British Police Stop
24/7 Monitoring of
Julian Assange At
Ecuadorian Embassy
Ion-Based Data
Allows Atom-Sized
Storage Cells
Similar To Brain
Google Releases
Improved Cardboard
SDK and Adds Street
Hi-Tech Body
Implants and the
Biohacker Movement
Why NASA Rejected
Lockheed Martin"s
Jupiter For
Commercial Resupply
Services 2
Can a New Type of
School Churn Out
Developers Faster?
Star Trek: New
Voyages, The
Fan-Based Star Trek
Article viewer

ACPI Battery Check

Written by:ateam
Published by:optikal
Published on:2005-04-10 21:53:02
Search OSI about Perl.More articles by ateam.
 viewed 15252 times send this article printer friendly

Digg this!
    Rate this article :
One of my Christmas presents this past year was a nice, 4 lbs. laptop. I use the Enlightenment window manager under Linux, thus, when my laptop's battery is getting too low, I don't have any application in my face warning me about it, like with KDE or Windows. Thus, I wrote a script to check the status of my battery and determine whether or not anything needs to be done about it. This is a short guide on using my script with your laptop.

ACPI Battery Check

Requirements: Linux kernel with ACPI support

This guide assumes you have a laptop, Linux, and something between your ears (preferably a brain). The script I've written will do the job on any machine with any battery running any Linux distribution. I'm wonderful, I know.

I feel its right to explain why I wrote this script in the first place, so here it goes... One day I was sitting in class playing a game on my laptop when suddenly it turned off. This came without warning, so I didn't get a chance to save my game, successfully making those past 20 minutes a waste of time. Perhaps I was being tought a lesson from you know who that in class, I should be doing something other than playing computer games. However, I'd like to believe it was just a way to get me to finally write a script to check my battery state and do something about it if need be!

Now, I know that there are tons of full-featured ACPI scripts out there. Most, if not ALL, of these have things written into them that I either don't want or that my laptop doesn't support. XScreenSaver already takes care of putting my display on standby and such and my ACPI modules already take care of fan usage, so why bother using some huge, bloated script? If you want a script like that, then what I've written isn't for you.

Okay, enough talk, its time for some code...

Once you have ACPI support in your kernel, /etc/acpi should be created automatically. If not, create it and an events directory and file as root like so:

mkdir /etc/acpi
mkdir /etc/acpi/events
touch /etc/acpi/events/default

If /etc/acpi and the other files already exist, then doing that is not needed. Regardless, /etc/acpi/events/default must be modified. Add the following lines into that file as root:


Now it comes time to put that battery_check.pl script in /etc/acpi, as specified in the event-rules above. First, create /etc/acpi/battery_check.pl as root like this:

touch /etc/acpi/battery_check.pl

Then, open /etc/acpi/battery_check.pl with your favorite text-editor and paste the following into it:

#Used to check ACPI battery state in /proc/acpi
#and warn users or halsts system if charge is
#too low.
#Written by ateam (derek@backdrifts.net)

$warning_message = "ACPI - Battery critically low!";
$halt_message = "ACPI - Battery too low, halting system NOW!";

chop (@pts = `ls /dev/pts/`);
($pts_length = @pts) --;

chop (@bat_state = `cat /proc/acpi/battery/BAT0/state`);
chop (@bat_info = `cat /proc/acpi/battery/BAT0/info`);

$charge_state = @bat_state[2];
$charge_state =~ s#(charging state: )##g;

$remaining = @bat_state[4];
$remaining =~ s#(remaining capacity: )##g;
$remaining =~ s#( mAh)##g;

$warning = @bat_info[5];
$warning =~ s#(design capacity warning: )##g;
$warning =~ s#( mAh)##g;

$low = @bat_info[6];
$low =~ s#(design capacity low: )##g;
$low =~ s#( mAh)##g;

if ($charge_state eq "discharging" && $remaining <= $warning && $remaining > $low) {
        for ($i = 0; $i <= $pts_length; $i ++) {
                system "echo \"$warning_message\" > /dev/pts/@pts[$i]";
        system "echo \"$warning_message\" > /dev/tty1";
        system "logger \"$warning_message\"";
} elsif ($charge_state eq "discharging" && $remaining <= $low) {
        for ($i = 0; $i <= $pts_length; $i ++) {
                system "echo \"$halt_message\" > /dev/pts/@pts[$i]";
        system "echo \"$halt_message\" > /dev/tty1";
        system "logger \"$halt_message\"";
        system "/sbin/init 0";
} else {
        system "logger \"ACPI - battery ok, no warning sent, system not halting\"";

If your perl interpreter binary is located somewhere other than /usr/bin/perl, then modify the code appropriately.

In my opinion, code is worth nothing to a user if he deson't understand how it works. In addition, I don't want you to be running some script on your machine that you're unfamiliar with. So, I'll explain.

Very simply, the script collects information regarding your battery's capacity, warning level, low level and state. When an ACPI event occurs, which is less than every minute, this script is run, checking your battery status. If your battery has reached its warning level, then a message is sent to tty1 and all virtual terminals (pts/*) warning you with "ACPI - Battery critically low!" If your battery is so low that your system needs to be halted in order to protect your hard disks, then the message will read "ACPI - Battery too low, halting system NOW!" All activity is logged in /var/log/messages to ensure that you know what's going on, or more importantly, what went on.

About modification of this script; if you don't wish to monitor BAT0, then change the script accordingly. In addition, you may not want your machine to be halted for you if your battery is about to die. I reccomend that you do, however.

Enjoy :)

Did you like this article? There are hundreds more.

2005-04-11 04:07:07
Useful, well written article :-) keep it up ateam. If you get your apache log monitor done that would make a great article I think :-)
2005-04-11 10:39:55
on a slightly different but unrelated note .. i found myself questioning the 'stepping' of my dell notebook the other week, i was convinced it was running at the wrong cpu speed when i thought i needed some muscle doing some software building.

whilst i was routing around looking for information, i found this application called speedswitchXP which allows you to override the windows xp cpu stepping, and switch your cpu into full, low or dynamic performance manually.

im not sure its good for the life of my notebook, or my power consumption, but theres something nice about thinking you can ramp your CPU up to the max.
2005-04-25 09:47:30
Although it is cool to see this written in perl, are there not bash script versions produced with the acpi packages for most linux distributions ?

Only I would imagine that a bash script would use less CPU cycles, and therefore less power ... which is an issue if you have a speedstep processor and you're running the script every minute or so. I'll test it out on my Thinkpad a little later ...

That said ... there are a whole lot of other ACPI events on which it would be cool to run perl scripts :)
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:

Blogs: (People who have posted blogs on this subject..)
List the number of files in a directory and each subdirectory on Fri 29th Sep 11am
I needed to get the number of files in a directory and recurse through all subdirectories doing the same. I nedded it so i could compare two sets of directory trees for something I was working on. My Irish chum Enstyne came up with this Perl solution w

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Perl: The basics by sefo

Basic knowledge on often used Perl features

Your Ad Here
Copyright Open Source Institute, 2006