27752 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

<strong><a href="http:/ /it.pens74.r u/">penna Montblanc</a ></strong> | <strong><a href="http:/ /it.pens74.r u/">penna Montblanc</a ></strong> | <strong><a href="http:/ /www.pens74. ru/it/">penn a Montblanc</a ></strong><b r> <title>Montb lanc Meisterstuck Finelin
<strong><a href="http:/ /it.copacosm etics.com/"> orologi</a>< /strong> | <strong><a href="http:/ /it.copacosm etics.com/"> orologi</a>< /strong> | <strong><a href="http:/ /www.copacos metics.com/i t/">orologi< /a></strong> <br> <title>Repli ca Tag Heuer orologi , im
[b][url=http ://it.watchr eplica.cn/]r eplica orologi di<strong><a href="http:/ /it.watchrep lica.cn/">re plica orologi di alta qualità</a> </strong><br > <strong><a href="http:/ /www.watchre plica.cn/it/ ">replica orologi di alta qualità</a> </strong><br >
<ul><li><str ong><a href="http:/ /www.watchre plica.cn/it/ ">swiss orologi meccanici movimento replica</a>< /strong></li ><li><strong ><a href="http:/ /it.watchrep lica.cn/">or ologi</a></s trong></li>< li><strong>< a href="http:/ /www.watchre plica.cn/it/ ">orologi</a ><
[b]<a href="http:/ /www.rolexwa tches.top/it /">swiss r<strong><a href="http:/ /www.rolexwa tches.top/it /">swiss replica orologi aaa +</a></stron g><br> <strong><a href="http:/ /www.rolexwa tches.top/it /">Orologi svizzeri replica</a>< /strong><br>

Donate and help us fund new challenges
Due Date: Nov 30
November Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00

News Feeds
The Register
VPN users menaced
by port forwarding
LHC records biggest
bang ever with 1
EU privacy watchdog
calls for more
?processing of
personal data?
Walmart spied on
workers" Tweets,
blogs before
Amazon"s new drones
powered by Jeremy
Clarkson"s sarcasm
OLPC"s modular heir
hits the
crowdfunding trail
Is it a Loon or is
it a drone? Google
seeks experimental
radio license in US
Estonian vendor
sparks Li-Fi
hypegasm with
gigabit demo
Microsoft takes
PUPs behind the
shed with gun in
Google cloud outage
caused by failure
that saw admins run
it manually ... and
Diamond Nanothreads
Could Support Space
VW Officials Knew
Since Last Year of
Misleading Fuel
Economy Claims
Israel Meets With
Google and YouTube
To Discuss
Censoring Videos
Contractors or Not,
Seattle Uber
Drivers Might Get
Amazon Reveals New
Delivery Drone
Design With Range
of 15 Miles
Purdue Experiments
Student Loans
Facebook Expands
Parental Leave
Policy For All
Employees Globally
Ethics: A Good
Reason To Sit
Further Away From
Your Boss
Cortana Coming To
iOS, For 2000 Beta
Canadian, UK Law
Professors Condemn
Space Mining
Provisions of
Commercial Space
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 15370 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