The Notebook Review forums were hosted by TechTarget, who shut down them down on January 31, 2022. This static read-only archive was pulled by NBR forum users between January 20 and January 31, 2022, in an effort to make sure that the valuable technical information that had been posted on the forums is preserved. For current discussions, many NBR forum users moved over to NotebookTalk.net after the shutdown.
Problems? See this thread at archive.org.

    battinfo a script to show battery info and wear from the command line

    Discussion in 'Linux Compatibility and Software' started by Sxooter, Aug 24, 2011.

  1. Sxooter

    Sxooter Notebook Virtuoso

    Reputations:
    747
    Messages:
    3,784
    Likes Received:
    8
    Trophy Points:
    106
    So, if anyone wants it, I threw together this simple script to show battery status including if it's charging or discharging, charge / discharge rate, current wear level, and time left to charge or discharge at current rate.

    Code:
    #!/bin/bash
    bats=`ls /proc/acpi/battery/`;
    for bat in $bats;do 
    	remain=`cat /proc/acpi/battery/$bat/{info,state}|grep remaining|grep -o "[0-9]\+"`
    	full=`cat /proc/acpi/battery/$bat/{info,state}|grep full |grep -o "[0-9]\+"`
    	descap=`cat /proc/acpi/battery/$bat/{info,state}|grep "design capacity:" |grep -o "[0-9]\+"`
    	rate=`cat /proc/acpi/battery/$bat/{info,state}|grep "present rate:" |grep -o "[0-9]\+"`
    	state=`cat /proc/acpi/battery/$bat/{info,state}|grep "charging state:" |grep -o "[a-z]\+$"`
    	echo $state;
    	lostcap=$((((descap-full)*100)/descap));
    	echo "Lost Capacity:" $lostcap"%"
    	percfull=$(((remain)*100/full));
    	echo "Percent Full: " $percfull"%"
    	if [[ "$rate" -ne 0 ]]; then
    		if [[ "$state" = 'discharging' || "$state" = 'charged' ]]; then
    			timeleft=$((((remain)*60)/rate))
    			echo "Time left to run: "$timeleft" minutes"
    		else
    			timeleft=$((((full-remain)*60)/rate))
    			echo "Time left to full: "$timeleft" minutes"
    		fi
    	fi;
    done
     
  2. TuxDude

    TuxDude Notebook Deity

    Reputations:
    255
    Messages:
    921
    Likes Received:
    2
    Trophy Points:
    31
    Nice cool script :)

    Just one suggestion, you can store the output of 'cat /proc/acpi/battery/$bat/{info,state}' in a variable instead of reading it every time and echo that variable and pipe it to grep.

    eg.
     
  3. Sxooter

    Sxooter Notebook Virtuoso

    Reputations:
    747
    Messages:
    3,784
    Likes Received:
    8
    Trophy Points:
    106
    Heck, just post a patched version! :)
     
  4. debguy

    debguy rip dmr

    Reputations:
    607
    Messages:
    893
    Likes Received:
    4
    Trophy Points:
    31
    What kernel do you use?
    /proc/acpi/battery is considered deprecated since somewhere between 2.6.18 and 2.6.26 in the vanilla kernel.

    The new location (with changed structure) is:
    /sys/class/power_supply/battery
     
  5. TuxDude

    TuxDude Notebook Deity

    Reputations:
    255
    Messages:
    921
    Likes Received:
    2
    Trophy Points:
    31
    Yeah it is deprecated at least for /proc/acpi/battery and /proc/acpi/ac_adapter. But it can be still used if the CONFIG_ACPI_PROCFS_POWER=y is set in the kernel .config, which is what most distros do. It is done because there are many programs in the system which still make use of the /proc interface for battery related stuff.

    More info:
    Linux kernel configuration help for CONFIG_ACPI_PROCFS_POWER in 2.6.34 on x86

    So you could modify your script such that if it finds /sys interface first, use that otherwise stick with /proc.

    Also, you can add one more check where the acpi directory exists but battery and ac_adapter directories wont exist (in case of desktops) - and print a message "No battery found" or something similar...
     
  6. Sxooter

    Sxooter Notebook Virtuoso

    Reputations:
    747
    Messages:
    3,784
    Likes Received:
    8
    Trophy Points:
    106
    Hey! Patches people! haha, just kidding, I can look at changing it. I'm on Ubuntu 10.10 and it doesn't have a /sys/class/power_supply/battery directory on it, so I'm not familiar with the layout of that if it's any different. But it's easy enough to make the changes. also, I know for a fact some parts of this will break on my Acer AO722 which doesn't give current charge rate when it's charging. So no estimating time til charged.