Python Script to Monitor Laptop Battery Charge (Ubuntu/Linux Mint)

I got my laptop two days ago. It’s a Compaq Presario CQ42-220TU. Battery backup is around 3 hours. I am using Linux Mint which is a variation of Ubuntu. Just a few minutes ago I wrote a Python script that monitors the battery charge and if it’s discharging, it gives a notification with remaining percentage of charge every 5 minutes.

Here’s the source codes for the curious minds πŸ˜‰

Download:http://code.google.com/p/masnun/downloads/detail?name=battery.py

For convenience, I added the script to my start up applications. So whenever I log in to my lappy, it gets auto started. If you want to use this script as well, save the above source code in a file. Let’s assume you saved it in a file named “battery.py” in your home folder. Now give it execution permission. Right click on the file. Go to properties. In the “Permissions” tab, tick the option saying “Allow executing file as program”. Close the dialog box.

Now go to “Preferences” menu from Sytem menu (Ubuntu) or Mint Menu (Linux Mint). Go to Startup Applications. Click Add. Type in a name and comment. In the command field, type the full path of the file. Eg. /home/<_username_>/battery.py.

Restart your PC. πŸ™‚

If you can’t get it working, please provide feedback here or catch me on IM. My contact details are available on http://masnun.com.


12 Comments Python Script to Monitor Laptop Battery Charge (Ubuntu/Linux Mint)

  1. Tushar

    Wow..Script is cool. Just uses system acpi to get values.. Liked it.. πŸ˜€ Simple easy but useful. Can be integrated into other app. Keep up masnun.

    Reply
  2. Joy

    hellow bro, i m looking forward to buy the same laptop u have. can you please tell me whether buying it would be a good deal or not. or if you are facing any problem with this laptop, please let me know. thank you.

    Reply
  3. runoverdog

    Man you save my day!!
    I need to check low battery state because I’m using XMonad since a few weeks and XMobar shows battery status but doesn’t notify low battery level, so I made a few changes to your original script. I’ll send it to you, feel free to change it, use it or to distributed it.

    Reply
  4. Prasanna Venkadesh

    Hey there,
    Thanks a lot man.. Your script is very useful indeed. Similar to what Eliezer Faizal did, i too modified it to alert me whenever charge is full and unplug my cable πŸ™‚ But my laptop is not ACPI compatible and it does not contains battery state in /proc system instead i used /sys system and get it done. If there are anyone similar who cannot find battery info from /proc, tel me i could send you the code which uses /sys

    Reply
  5. Joy

    hello,
    um using the same notebook, and facing the same issue with battery backup time. Since um nt as tech savvy as u ppl, can any1 suggest me any software for my windows pc which will help me to solve this problem.

    Reply
  6. Babo

    Hi
    i was really in search of such a code, i just have a small question .. i have a seprate python script running , and i want to add a flag value in that script which will take value=1 when the laptop battery is less then 10% Β can you give me some hints how can i do that while utilizing this script.. im using ubuntu aswell

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *