A Lightweight Low-Battery Warning Monitor for GTK+ 3.x desktops

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:

sudo apt-get install python-notify2 python-gi gir1.2-upowerglib-1.0 gir1.2-gstreamer-1.0 gir1.2-gst-plugins-base-1.0

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.

CC BY-SA 4.0 A Lightweight Low-Battery Warning Monitor for GTK+ 3.x desktops by Stephan Sokolow is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

This entry was posted in Geek Stuff. Bookmark the permalink.

10 Responses to A Lightweight Low-Battery Warning Monitor for GTK+ 3.x desktops

  1. Guest says:


    • 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.

  2. Zetro Track says:

    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).
    4) etc.
    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 🙂

  3. Pingback: A Lightweight Low-Battery Warning Monitor for GTK+ 3.x desktops - Yet Another Linux Blog

  4. Max says:

    Lightweightest 🙂 notification system. https://github.com/sboli/twmn

  5. 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.


  6. Jonathan says:

    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?

  7. 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.)

  8. Zhong says:

    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.

Leave a Reply

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

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution under the same terms as the associated post.

All comments are moderated. If your comment is generic enough to apply to any post, it will be assumed to be spam. Borderline comments will have their URL field erased before being approved.