My *nix world

X11 battery status

If you are using a laptop (like me) then you know that is important to be notified (eg. a message) whenever your AC is plugged/unplugged or when your battery has less than 30% power (or whatever % triggers you). I think all the Linux distro have an applet/pluggin which can take care about this by usually they consume more resources than my simple solution I propose bellow:

  • a simple bash script (eg: battery_status) which check every 5 seconds (customizable) the battery status a print a notification balloon on your screen
  • load that script at session start up (so it will run as long your session runs) so it will monitor your battery status all the time

The script:

#!/bin/bash
#####################################################################
# Script for fetching and displaying battery information
# You can define a desktop keyboard shortcut and launch this at user
# request or you can autostart this at session login.
#
# Author        : Eugen Mihailescu
# Last change   : 21.Feb.2013
# E-mail        : eugenmihailescux at gmail dot com
#
# Depends on    : x11-libs/libnotify
# Tested on     : Linux 3.6.10-gentoo-2.1 x86_64 GenuineIntel
#####################################################################

ALERT_LIMIT=30 # when level is < 30% alert every -5% (ALERT_LIMIT_FREQ) ALERT_LIMIT_FREQ=5 BATTERY="quot;C1E9"quot; ARG=$1 GetBatteryInfo() { 	awk -F "quot;="quot; -v param=$1 '$0~param {print $2}' /sys/bus/acpi/drivers/battery/PNP0C0A:00/power_supply/$BATTERY/uevent } old_level=100 while :;do 	let level=100*$(GetBatteryInfo "quot;POWER_SUPPLY_CHARGE_NOW"quot;)/$(GetBatteryInfo "quot;POWER_SUPPLY_CHARGE_FULL"quot;) 	if [ level > 50 ];then
		icon="quot;/usr/share/icons/Tango/48x48/devices/battery.png"quot;
	else
		icon="quot;/usr/share/icons/Tango/48x48/status/battery-caution.png"quot;
	fi
	status=$(GetBatteryInfo "quot;POWER_SUPPLY_STATUS"quot;)
	if [ "quot;$status"quot; == "quot;Unknown"quot; ] "amp;"amp; [ $(GetBatteryInfo "quot;POWER_SUPPLY_CURRENT_NOW"quot;) -eq 0 ];then
		status="quot;Charging"quot;
	fi
	display_status=0
	if [ "quot;$status"quot; != "quot;$old_status"quot; ];then
		display_status=1
	fi
	level_changed=0
	if [ $(($old_level-$ALERT_LIMIT_FREQ)) -ge $level ];then
		level_changed=1
	fi
	if [ $level -lt $ALERT_LIMIT ] "amp;"amp; [ $level_changed -eq 1 ] || [ $display_status -eq 1 ];then
		name=$(GetBatteryInfo "quot;POWER_SUPPLY_NAME"quot;)
		technology=$(GetBatteryInfo "quot;POWER_SUPPLY_TECHNOLOGY"quot;)
		manufacturer=$(GetBatteryInfo "quot;POWER_SUPPLY_MANUFACTURER"quot;)

		/usr/bin/notify-send -i $icon "quot;$manufacturer $name $technology"quot; "quot;Level : $level%nStatus : $status"quot;
		old_level=$level
	fi
	old_status=$status
	if [ $ARG = "quot;q"quot; ];then
		break
	fi
	sleep 10
done

Start the script at session start-up

In Xfce you can set up a Application Autostart (which are supposed to start at session start up):

  • Settings -> Session and Startup -> Application Autostart tab
  • define a new application autostart as bellow:
    • name: battery_status (in fact it doesn't matter how you will name it)
    • command: battery_status (specify also the path if the script cannot be found on PATH)

So, the script will start when your X session starts and then will run in background every 5 seconds (or whatever ALERT_LIMIT_FREQ you would like).

Depending on the battery status a notification balloon like the one bellow can be shown:

battery status
Edit 21.Feb.2013: I've changed a bit the original script and the current version (above) allows one to invoke this script followed by an immediate exit. That way, in addition to the script main loop that track your battery status, one could invoke the script with an argument "q" which will display the current status then exit:

battery_status q

So I've created a desktop shortcut (like CTRL+ALT+B) that shows immediately the battery status then leave, while the session script instance still running in background, watching the battery status and informing me whenever necessary.

Now, if you think that this article was interesting don't forget to rate it. It shows me that you care and thus I will continue write about these things.

The following two tabs change content below.
X11 battery status

Eugen Mihailescu

Founder/programmer/one-man-show at Cubique Software
Always looking to learn more about *nix world, about the fundamental concepts of math, physics, electronics. I am also passionate about programming, database and systems administration. 16+ yrs experience in software development, designing enterprise systems, IT support and troubleshooting.
X11 battery status

Latest posts by Eugen Mihailescu (see all)

Leave a Reply

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