Today, I decided to help someone out and, as is often the case with coding, I got carried away.
I set out to help fix up a shell script but, instead, I decided to “do it right” and produced a simple Python script which…
Uses a combination of GLib.timeout_add_seconds and UPower events to react to power supply changes immediately while still putting a limit on how often battery percentage changes should be able to cause wakeups.
Uses GStreamer and notify2 to provide audio and visual low-battery warnings without the weight and delay of calling subprocesses.
Is compact and easy to customize because it’s written in Python
By design, it doesn’t really have much of a UI (just a persistent notification popup and alert sound that come and go in response to changes in power state), but here’s what the popup looks like when using xfce4-notifyd:
It does require the 1.0 UPower API for its “Display Device” abstraction, so an 0.99.x-series version of UPower or newer is required, but for people on a new enough Debian/Ubuntu/Mint release (Vivid, in Ubuntu’s case), the dependencies can be installed as follows:
It hasn’t been tested under Python 3.x but it looks like it should work perfectly if you just replace python-notify2 python-gi with python3-notify2 python3-gi in the dependencies and edit the shebang line at the top of the file.
I only wish I’d realized what I was doing sooner so I could start tracking its evolution properly in git. As-is, all I could do was translate the snapshots from Dropbox’s revision history.
As usual for small, single-file things, here’s the direct download link followed by a GitHub Gist embed.
There you go. I’ve updated the post. As it now says, by design, there isn’t much to screenshot. Just an audible alarm and a persistent popup telling you how much battery is left.
Great work. Thanks a lot buddy 🙂
The best thing is that it will force you to plug charger, before the battery is empty and you lose your valueable work. As it is written in python so you can customize it in your way. You can:
1) Change the minimum threshold level of battery charging.
2) Customize the the popup notification and its timings.
3) Change the audible sound (if you don’t like the existing one).
The customizations are endless. You can even add it to “Startup Applications” and it will take care of the rest and then you don’t have to see the battery charging again and again.
Thanks ssokolow 🙂
Pingback: A Lightweight Low-Battery Warning Monitor for GTK+ 3.x desktops - Yet Another Linux Blog
Lightweightest 🙂 notification system. https://github.com/sboli/twmn
For people who are zsh junkies, I just found this nifty project which adds a Zelda-style “health bar” battery monitor and includes instructions for getting automatic refresh under zsh.
I note that the python instance to run this script is taking 239MB — is there a better way to run this so that it is part of something else being done by the python engine and not using all these resources for this tiny script?
Dumb memory monitors don’t account for memory shared between multiple processes and the vast majority of that is probably shared libraries (eg. GTK+, UPower, libdbus, etc.).
This StackOverflow answer has various ways to determine how much memory the process is actually using. (eg. Tools which estimate memory consumption by dividing shared memory by the number of processes sharing it, tools which show only the memory consumption which can’t be shared, etc.)
Nothing seems to happen when I run the script: python battery_monitor.py and hangs with the message Battery Monitor is starting. Press ctrl C to exit……….
This is on the Debian Jessie with the default Gnome.
Sorry for the delayed reply. The last week’s been rather busy.
If I understand your description properly, that’s normal. It’s designed to remain silent and invisible until your battery level drops too low.