X11 battery status


Please note that this blog has been moved.

Now it has its own domain: mynixworld.info🙂

If you want to read the latest version of this article (recommended) please click here and I open the page for you.

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="C1E9" ARG=$1 GetBatteryInfo() { 	awk -F "=" -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 "POWER_SUPPLY_CHARGE_NOW")/$(GetBatteryInfo "POWER_SUPPLY_CHARGE_FULL") 	if [ level > 50 ];then
		icon="/usr/share/icons/Tango/48x48/devices/battery.png"
	else
		icon="/usr/share/icons/Tango/48x48/status/battery-caution.png"
	fi
	status=$(GetBatteryInfo "POWER_SUPPLY_STATUS")
	if [ "$status" == "Unknown" ] && [ $(GetBatteryInfo "POWER_SUPPLY_CURRENT_NOW") -eq 0 ];then
		status="Charging"
	fi
	display_status=0
	if [ "$status" != "$old_status" ];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 ] && [ $level_changed -eq 1 ] || [ $display_status -eq 1 ];then
		name=$(GetBatteryInfo "POWER_SUPPLY_NAME")
		technology=$(GetBatteryInfo "POWER_SUPPLY_TECHNOLOGY")
		manufacturer=$(GetBatteryInfo "POWER_SUPPLY_MANUFACTURER")

		/usr/bin/notify-send -i $icon "$manufacturer $name $technology" "Level : $level%\nStatus : $status"
		old_level=$level
	fi
	old_status=$status
	if [ $ARG = "q" ];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.

About Eugen Mihailescu

Always looking to learn more about *nix world, about the fundamental concepts of arithmetic, algebra and geometry. I am also passionate about programming, database and systems administration.
This entry was posted in linux, X11 and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s