Simple critical battery level warning script

This is a little script I wrote for use on my laptop.  I wanted to have some kind of visual cue to let me know when the battery charge gets below a certain level and this is what I came up with:
#!/bin/bash
# Configuration
interval=120 #in seconds
critical_level=10 #percent
icon="/usr/share/icons/Tango/48x48/devices/battery.png" #notification icon
battery_id="BAT1" #ACPI battery identifier
ac_adapter_id="ADP1" #ACPI power adapter identifier
while true
do
if [ "$(cat /proc/acpi/ac_adapter/$ac_adapter_id/state | grep -o off)" == "off" ]; then
battery_max=`cat /proc/acpi/battery/$battery_id/info | head -3 | tail -1 | awk '{print $4}'`
battery_current=`cat /proc/acpi/battery/$battery_id/state | head -5 | tail -1 | awk '{print $3}'`
battery_level=$((100*$battery_current/$battery_max))
[ $battery_level -le $critical_level ] && \
notify-send -u critical -i "$icon" -t 15000 \
"Battery level is low!" "Only $battery_level% of the charge remains."
fi
sleep $interval
done
This script uses notification-daemon (or notification-daemon-xfce) package.  It can be configured by editing values in the "Configuration" section.  I just autostart it with my openbox session.  It all should be pretty self-explanatory otherwise. 
I hope someone finds it useful.
EDIT: Some improvements to the script.
Last edited by fwojciec (2008-07-09 13:11:14)

robmaloy wrote:
thx, really cool i always forget to look after my battery percentage
maybe you can replace "BAT1" and "ADP1" with variables, so configuration would be easier
Thanks   I made changes according to your suggestion.
moljac024 wrote:laptop-mode tools has an option of running commands when battery charge reaches a certain level. You could just run notify-send from there.
You mean in the auto-hibernate section?  I use it as it was supposed to be used -- for hibernating -- and I think you can only set one command that's executed according to a specified battery level in laptop-mode-tools.  Plus it would be sort of complicated, since displaying the notification depends on two conditions in this script -- specified battery level and power cord being disconnected.

Similar Messages

  • Cannot lower Critical Battery Level in Power Options

    I am wanting to adjust the Critical Battery Level in Power Options to something lower than the default 7% that HP has set. When I try to change the number, it will not save and it just magically jumps back to 7%. This means that my laptop will hibernate when the battery level reaches 7% and I cannot turn it back on unless I plug it into AC power. 7% equates to roughly 40 minutes of battery time and I find this setting way too conservative.
    I have done some research on this issue and have found the following:
    Some suggest that PC manufacturers will somehow lock this setting to an arbitrary minimum and the user cannot change it to anything below this minimum (it appears HP have set it at 7% in my instance).
    This link suggests using power.cfg and the command line to make the changes How To Lower the Critical Battery Level to 1% in Windows 7
    Steps I have taken to try and resolve this:
    I have tried the powercfg.exe method described in the aforementioned link to change the critical battery level to 3% and initially it appears to work. When I open up the Power Options screen, it indeed says 3%. However, when I actually test it out by using the laptop, it will still automatically hibernate at 7% battery level - it simply ignores the 3% setting.
    I have also tried using powercfg.exe to change the Critical Battery Action to "Do Nothing" and likewise this setting is ignored and the laptop hibernates anyway.
    I have phoned HP support and the only useful information that I got from them so far, is that the PC manufacturer has set it to 7% and it cannot be changed. They said they will escalate this matter to the research team and give me a call back.
    I phoned Microsoft support and the consultant seemed to be confident that he could fix the issue but would not divulge the answer because he said that doing so would void the HP warranty and he didn't want to be to one to do that. He suggested that I call HP and request a conference call with Microsoft and they could then fix it. He seemed to be of the belief that if HP has set the 7% minimum that they should be the one to remove the limit.
    If nothing works, I wonder if completely formatting the laptop (including removing the HP recovery partition) and then reinstalling a different Windows 8.1 (or Windows 7) will let me have full customization of the Critical Battery Level?
    I would really appreciate your thoughts on how to fix this. It really fustrates me that HP thinks they know better and that the user needs to have 7% battery reserved - it is such a waste not to be able to utilize a battery to its full potential.

    Hi @Sparkles1 ,
    There must be an alternative solution to this.
    What is the reasoning behind HP locking this setting at 7% minimum?  I understand that a battery should not be run down to 0% as this will damage the battery but 7% is unreasonably high and in fact this does not conform with ACPI standard specification.  I refer to the following document: https://msdn.microsoft.com/en-us/library/windows/hardware/dn481320%28v=vs.85%29.aspx
    In particular, please see the table where it says that the critical battery level must be set to a value between 0 and 5%
    Can you please advise how to change this?  I believe this is a software related issue (specifically, the battery drivers).  Can you issue an alternative driver to install?  If you insist that it cannot be done, I will have no other option than to return the laptop and request a full refund.

  • Critical Battery Level shutdown on desktop after CMOS replace...

    So after speaking with an HP tech.. she told me to go buy a replacement battery and described how to do it.. no problem.. so i go ahead.. but find that my computer is still restarting with the "critical battery reached blah blah blah"  business as before.. anyone else run into this BS???

    somebody has to have some answers lol.. its happened everyday the last 3 days, today while i was in the middle of a playing a game. just shutdown, reboot with the "battery has reached critical state" message upon startup..  again, HP support had me replace the battery on the motherboard, did that, and its still happening.. not a notebook, a desktop.. make and model are posted above.. plz help anyone lol.. thx in advance.

  • Critical Battery Level reached, forced restart on my desktop

    Hello all.  My HP Pavilion e9180t will suddenly shutdown and restart at random times.  Upon restart it says that it was shutdown due to the battery reaching critical levels. I spoke with a representative over the phone and she had me change my CMOS (?) battery.  So i bought one and replaced that, its still happening though.  Any help would be fantastic..
    HP Pavilion e9180t  64bit
    Intel Core i7 [email protected]
    Windows 7 Home Premium
    1TB HD   9GB RAM

    Hi,
    Remove the battery and check the voltage with a volt-ohm-meter.  It's possible that the replacement is not at 3+ volts. At 2.5 volts you might start seeing issues and at less than 1.8 volts the battery is at the critical level.
    You should be using a lithium CR2032 battery.  You might see close to 3.3v on a fully fresh CMOS battery.
    If your PC is running on UPS then this could also cause an error message. Check the electrical power to the UPS and/or by-pass the UPS to determine if the UPS is the issue.
    HP DV9700, t9300, Nvidia 8600, 4GB, Crucial C300 128GB SSD
    HP Photosmart Premium C309G, HP Photosmart 6520
    HP Touchpad, HP Chromebook 11
    Custom i7-4770k,Z-87, 8GB, Vertex 3 SSD, Samsung EVO SSD, Corsair HX650,GTX 760
    Custom i7-4790k,Z-97, 16GB, Vertex 3 SSD, Plextor M.2 SSD, Samsung EVO SSD, Corsair HX650, GTX 660TI
    Windows 7/8 UEFI/Legacy mode, MBR/GPT

  • PlayBook battery level warning does not always work and shuts down without warning

    My BlackBerry PlayBook, operating on the new OS2, annoyingly shuts down at 12% battery life without any warning if I have anything open.  If open applications are reduced so that the desktop is visible, the PlayBook will alarm at 9% and and continue to run below this figure.   Does anyone have a solution to this or experienced this issue?  Thank you.

    Your Macbook is still under warranty.
    I would call Apple Support and see what they recommend. You don't have to purchase the AppleCare unless you want to.
    You have another choice.
    You can buy a new battery and see if that works for you.
    http://store.apple.com/1-800-MY-APPLE/WebObjects/AppleStore.woa/wa/RSLID?mco=F80 3127E&nplm=MA561G%2FA
    Then you could take in the original battery and have it checked out under warranty.
    Of course, you would probably end up with two batteries in the end but sometimes that is a good thing depending on how much you travel.
    You might also want to see this post:
    http://discussions.apple.com/message.jspa?messageID=3963944#3963944

  • Critical Battery Suspends Incorrectly

    I'm using gnome-power-manager, and my computer suspends wonderfully. I'm not sure which method I'm using since I tried both and I cannot recall what I did on my last effort to get suspend/hibernate to work. However, after my laptop suspends for being idle, charges down past critical battery level, I start it up. I made the critical battery level mark 5%, by the by, so that it can suspend for a good while without me noticing.
    Anyway, I start it back up at a power level bellow critical and it doesn't complain. This is one issue. I feel like this behavior is the wrong one. Can someone explain to me why it is not or why I should look at it another way? Or should something be filed upstream-ish/to the package manager?
    As I leave the laptop on and plugged in, the battery charges. Upon reaching the 5% level, it tells me my battery level is critical and suspends. This is just plain wrong. If I'm charging and the battery level goes critical, it should not suspend. I feel like I should post this to the package manager of gnome-power-manager. Thing is that I don't quite remember which package that is. Truly, I feel like I should post this to GNOME, but I think there's a pecking order for these things?

    majiq wrote:
    Anyway, I start it back up at a power level bellow critical and it doesn't complain. This is one issue. I feel like this behavior is the wrong one. Can someone explain to me why it is not or why I should look at it another way? Or should something be filed upstream-ish/to the package manager?
    As I leave the laptop on and plugged in, the battery charges. Upon reaching the 5% level, it tells me my battery level is critical and suspends. This is just plain wrong. If I'm charging and the battery level goes critical, it should not suspend. I feel like I should post this to the package manager of gnome-power-manager. Thing is that I don't quite remember which package that is. Truly, I feel like I should post this to GNOME, but I think there's a pecking order for these things?
    Both these issues sound like valid bugs to me and I would certainly up-stream them - especially the second.
    Arch is a pretty raw distribution in so much as it makes little or no modifications to the up-stream packages.  This will typically mean that you can report issues with packages directly up-stream - which most developers would prefer.  But you are right - if unsure it is a good idea to check to see if it is a distro specific issue first.

  • Critical battery shut down - at 50%!!!! (T440s

    Hi ,
    I bought a new Lenova Thinkpad T440s (20AQ006VAD) 2 days back. System shutdown when the battery level reaches 50% and does not wake up without connecting charger. critical battery level is set to 5% on power options. Can anyone help me to fix the issue

    Hi Nmshameem,
    Welcome to Lenovo Community!
    We understood that you are facing the issue with battery on your ThinkPad T440s.
    To resolve this issue please open power manage and go to Battery menu and click on Battery Maintenance, you will get a new window and click on Perform Reset under Battery gauge reset.
    Note: Please connect the power adapter while performing reset, and this process may take nearly 2 to 4 hours.
    Hope this helps. 
    Best regards,
    Hemanth Kumar
    Did someone help you today? Press the star on the left to thank them with a Kudo!
    If you find a post helpful and it answers your question, please mark it as an "Accepted Solution"! This will help the rest of the Community with similar issues identify the verified solution and benefit from it.
    Follow @LenovoForums on Twitter!

  • Playbook stuck on 'Battery Level Critically Low' screen.

    I know there's 100's of forums about the Playbook's battery problems. Just hear me out, please. Nearly 3 weeks ago, my Playbook died on me. It was stuck at 11% power, I tried charging with the charging it came with, nothing. When it went flat, I tried stack charging. No luck there either. Later, I bout a rapid travel charger, tried stack charging, nada. (I did get the LED activity and sceens the support page mentioned). Leaving it charging for a while, it was able to get the the home screen, but refused to charged. Since my Playbook was able to recieve power when connected to the rapid charger, I connected my Playbook via USB. I made a backup and I wiped the OS through the Desktop Manager because I thought my Playbook was bricked. My problem still wasn't fixed. I forgot that I still needed 20% power to install the OS, and since my Playbook refuses to charge, I'm stuck on a screen saying: 'Battery Level Criticaly Low' with a RED battery icon with a lightingbolt through it, with 0% on top of it, telling me to connect it to a power source, even known its connected to my Rapid Charger. I have no idea what to do now. Disconnecting the Rapid Charger immediatly shuts-off my Playbook. I also noticed something wierd. If connected to a Rapid Charger alone, when I disconnect it while a RED battery icon appears, it shuts-off right away, I don't get the icon telling me to reconnect. But when I have to connected to the Rapid Charger AND my PC via USB, I get an icon telling me to reconnect to a power source when I remove the Rapid Charger. My problem still remains. Even if a leave it charging while connected to my PC, and it boots-up, I still get the 'Battery Level Critically Low' screen. It still shuts-off when I disconnect the charger. Any tips? I've tried the Jump-Boost, stack charging and the 'Battery Characteristics' page wasn't much help either. My Playbook is still alive, I just need to find out how to make it charge. I'll draw a diagram of what's happening and post it here if you need it.

    Assuming the rapid charger is the three pin magnetic type made by Blackberry, that charger almost always will charge a good playbook. Disconnecting the rapid charger should give the same red battery icon, but it doesnt. As it seems neither route will charge it I suspect the battery, the battery strap or the motherboard has a dry joint. If you cant get to it and find it by removing the back of the playbook, I would assume its days are over.
    If so the backup will be of no use unless you can get another playbook. 
    Last resorts before getting out the spudgers...
    Leave on rapid charger overnight.  Press power button for 20 sec every so often next day whilst on charge.
    Or
    Leave connected to a non sleeping laptop usb overnight. Playbooks in standby will usually accept a trickle charge that way. 
    Sarah.

  • HT1476 Hi, I have an iPhone 4s running iOS 7.0.4 and the phone keeps going of whenver battery level drops to 20% and it give the warning. It turns off immediately after I dismiss the warning message. I LOCKED THE PHONE AT 21% and I was able to play music

    Hi, I have an iPhone 4s running iOS 7.0.4 and the phone keeps going of whenver battery level drops to 20% and it give the warning. It turns off immediately after I dismiss the warning message. I LOCKED THE PHONE AT 21% and I was able to play music to 4% which means there is no problem with the battery, this seems to be a programming error.
    Earlier suggestion include that the battery has a dead cell but it wouldnt keep playing music for over 2 hours it it had a dead cell.

    My issue with IOS 7.1 and higher is my Bluetooth for the phone still works...no problem but the music does not work with bluetooth...I have an Acura TSX 2010...Acura said there are no incidents reported, but me...I just upgraded to IPhone 6 and still no good....as soon as i hit the AUX button it say USB...not bluetooth...ugh

  • Battery Low Level Warning

    From what I've read, I have been having the same battery problems as most of you here. So after getting a replacement battery from an Apple Store, Im wanting to get the most out of my new one.
    Here's the thing, so somewhere in the middle of my battery failing and my computer randomly shutting down, the low level warning prompt, turned off. Where do turn the warning prompt to "yes" instead of "no"? I can't seem to find it, other than knowing its off when looking under the Power section in System Profiler.
    Thanks for the help. This forum is amazing.

    Thanks for the response. But I still have another question. Why, if the prompt is automatic, then does it read as "NO" in the power section of System Profiler. I have the new battery in and it JUST got to 100%. Im thinking about calibrating it right now. Would you suggest that being a good idea or not? I know how to calibrate it, but I dont know when the best time to start it first.
    Thanks again.

  • IPhone ~needs~ an AUDIBLE Battery-low warning during calls!!!

    Okay, I've been needing to do this for quite some time. I'm actually surprised to find no one else has made this complaint yet (that I've been able to find).
    I am absolutely sick and tired of my iPhone dying in the middle of a call because I was not alerted to the battery's status once it got to 20% or lower.
    Does it make sense to anyone else that this happens? You're on a call, to your knowledge the battery was full or reasonably full before but now that you're talking you can't see the screen and you're not thinking about it so you keep talking. A while later your call drops and as you start to curse AT&T once again for the miserable coverage & constantly dropping calls you see that it's not them after all, but your phone's dead battery.
    Come on Apple.... How hard can it possibly be to program in an AUDIBLE-IN-CALL alert that tells you that your battery is about to die. This is so basic and simple that I'm finding it hard to believe that this hasn't been addressed before now!!! Don't get me wrong, I love my iPhone. I will never need another phone as long as there is some variation of the iPhone in existence. But seriously, this is something that should have been dealt with AGES AGO.
    I don't feel like I should even have to be saying this, but when you are in a call, your phone is against your ear, or if you use a headset of some kind, your phone is likely in a pocket. A Battery-Low warning that comes up on the screen is NOT going to be visible. There NEEDS to be a way for the iPhone to notify the user of a Battery-Low status that ACTUALLY GETS THEIR ATTENTION, not simply to remind them AFTER they've got it charging again that, yes, now that the phone is charging and back up from 0%, that the battery is still in-fact below 20%....
    I really am a reasonably devoted Apple fan, but I am highly annoyed by this, because while I have had my iPhone 3G since the week they were released, whenever the above problem would happen to me I just assumed it would only be a matter of time before it got ironed out. I mean, come on, Apple's a smart company, and this is such a basic thing that lesser phones have had this programmed into their OS's going back to some of the very first cellular phones in existence, I know, I've owned a few.
    Apple, Please. Fix this.
    Thank you in advance.

    thegadson6 wrote:
    I just posted the same thing and it's now 2014!!!! I have endured this problem since my iphone 3gs.
    And the option is still the same.  Give feedback to Apple directly at www.apple.com/feedback.
    Nothing anyone can do here, other than recommend that you be more aware of your battery life.  Keep it connected to a power source when not in use, or if one is readily available when you are using it.  I have a charger for home, one for my car, and one at work.  I never see the battery low indicators, except in very rare cases.  It's very rare for my battery to be below 50%.

  • N97 mini battery level drops suddenly

    I've observed my N97 mini's battery level dropping abruptly from 4 bars to 1 bar (with low battery alarm). The phone's slightly more than a month old and using the latest 11.x.x firmware. Did a bit of googling, and found several suggestions on how to save battery (turn off everything 'smart' on ur phone). That's not the point anyways. I need to find out why the battery level drops suddenly (like what happens when ur battery's dying out), and *not* why the battery lasts X hrs. I'm not sure about this, but I think this mostly happens when the battery level is slightly below max, and I use any app that needs the GPS functionality. But then, there doesn't seem to be any problem with the GPS hardware in the phone, 'coz it usually gets a GPS lock in less than a minute (mostly within seconds). Could it be related to the algo used to show the battery level, or is it that its my battery that's faulty? Any others out there having the same problem?

    It's understandable that the batterymeter is not linear (atleast the charge/discharge rates are not). But what bugs me are those sudden erratic drops within minutes (a couple of minutes at times). Anyone who has spent a few days with a dying (rechargeable) battery would know what I'm talking about. It's not a good feeling when u r away from a charge source, and you decide to use one of those power hungry things (like Wifi/GPS) thinking you still have sufficient juice left...and then bam! ... the low battery warning! you just switch off everything, and hope that it lasts till you reach the next oasis. I guess I'd have to analyze this a bit more to understand if its some specific app that causes such behaviour (as mentioned in my original post, i doubt the GPS apps currently). If it happens too often, and even with 'normal' voice+text kind of usage scenarios, its time see Nokia Care I guess. The reason for this post is to know if this behaviour is observed by other N97 mini users as well..then I know there's no point in taking it to Nokia Care.

  • How to check battery level?

    Is there a way to check the battery level on the iPod Touch? I recently acquired a new 32 GB Touch, and the first thing I did was upgrade to the 2.0 software. I have been unable to find an option or menu that will tell me how much battery life I have remaining.
    In looking around I was able to find that on the iPhone there's a Settings > General > Usage menu, but I don't have that on my Touch.
    All I ever get is the 20% remaining warning message. Is there no other way to determine how the battery is doing?
    Thanks for the help...
    -Gary

    Hard to believe they don't have something that tells you where you're at. The icon is okay, but is not as helpful as an approximate percentage or number of minutes remaining. I realize battery life remaining can change drastically, say if you start watching a movie, but it would be nice to have something a little more substantial.
    I submitted feedback...thanks for the link.
    -Gary

  • Px4-300d shuts down/powers off while on UPS battery, regardless of remaining battery level

    Upon line power failure, the UPS is instantly available to supply power to the px4-300d.  The UPS is adequately sized to provide well over an hour of runtime during a power failure.  The only way to have caching operate is to have a UPS connected.  With the UPS connected, the NAS sees the UPS, the current power condition, and an accurate battery power level (%).  Once the power fails, the unit begins its shutdown process and completes gracefully, even though the remaining power in the UPS battery is still near 100%.  The whole point of having UPS backup is to keep the device running for as long as possible during a power failure, and then to gracefully shut-down the device should the UPS battery level reach a critical level and line-power still not restored.  This is MANDATORY to be addressed in the next firmware release, or provide access to a configuration file that specifies the battery level percentage which triggers a shutdown.  Why choose this "Enterprise Quality" storage solution if during a power outage it shuts itself down while everything else is still operating?  Makes absolutely no sense.
    Solved!
    Go to Solution.

    Hi easy-e,
    What is the firmware version on your device currently?
    LifeLine-based network storage devices should remain up and running normally using only UPS battery power until the UPS reports remaining battery is less than 3 minutes or below 5%. At his point, device shutdown will be triggered and your network storage device will gracefully shut down.
    Have you checked to make sure it is compatible?:
    What Universal Power Supply (UPS) models are compatible with my ix or px network device?
    APC UPS not working with network storage device
    If it is not shutting down properly, I would suggest contacting technical support to see if they can troubleshoot the cause of this problem.
    Have questions and need answers?
    Search the database for answers to FAQ's, software/driver downloads, tutorials, news, features and more!
    LenovoEMC Support & Downloads
    LenovoEMC North America Support Contact Page

  • MacBook shuts down at 30-35% battery level: any suggestion please?

    My MacBook (13" unibody) shuts down at 30-35% of battery level without any warning. I have the impression that it began to happen after I installed the Snow Leopard upgrade. I got the computer on Dec. 24th 2008. Any suggestions, please?
    This is the Battery power information and Hardware Overview on my MacBook:
    Battery Information:
    Model Information:
    Serial Number: 9G84706533NYB
    Manufacturer: DP
    Device name: bq20z951
    Pack Lot Code: 0000
    PCB Lot Code: 0000
    Firmware Version: 002a
    Hardware Revision: 0005
    Cell Revision: 0100
    Charge Information:
    Charge remaining (mAh): 1494
    Fully charged: No
    Charging: Yes
    Full charge capacity (mAh): 2697
    Health Information:
    Cycle count: 262
    Condition: Check Battery
    Battery Installed: Yes
    Amperage (mA): 511
    Voltage (mV): 12239
    Hardware Overview:
    Model Name: MacBook
    Model Identifier: MacBook5,1
    Processor Name: Intel Core 2 Duo
    Processor Speed: 2 GHz

    Hi azuzeno, welcome to the forums and happy holidays,
    About Batteries. http://www.apple.com/batteries/
    From //www.apple.com/batteries/notebooks.html
    "Battery Lifespan For Apple notebooks with removable batteries — such as previous generation MacBook and MacBook Pro computers — a properly maintained battery is designed to retain up to 80% of its original capacity at 300 full charge and discharge cycles. You may choose to replace your battery when it no longer holds sufficient charge to meet your needs.
    Your battery info state "Cycle count: 262" So your pretty close to the 300 cycle limit. Just like an older car, old batteries can behave erratically. It's time to buy a new battery.
    You could also book an appointment for the Genius Bar. http://www.apple.com/retail/geniusbar/ They may be able to save you a few dollars.

Maybe you are looking for