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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/env python import commands import pynotify from threading import Timer def battery_check(): rem = float(commands.getoutput("grep \"^remaining capacity\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'")) full = float(commands.getoutput("grep \"^last full capacity\" /proc/acpi/battery/BAT0/info | awk '{ print $4 }'")) state = commands.getoutput("grep \"^charging state\" /proc/acpi/battery/BAT0/state | awk '{ print $3 }'") percentage = int((rem/full) * 100) if state == "discharging": pynotify.init("Battery Alert!") notification = pynotify.Notification("Battery "+state,str(percentage)+"%","/usr/share/icons/gnome/32x32/status/battery-low.png") notification.show() timer = Timer(300.0,battery_check) timer.start() if __name__ == "__main__": battery_check() |
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 https://masnun.com.