Saturday, September 3, 2011

A simple laptop battery monitor script

The script displays alert when the battery falls below 20% or charges up beyond 80% which should be the optimal range of its working.

The code is as below. Just copy and paste and save as any file name but preferably The script can independent of what name it is given. Needs to have acpi (default install) and notify-send installed (sudo apt-get install notify-osd).



# battery-notify v1.2 by JOnes

# It is a script to monitor battery and keep it between 20% to 80% the optimal battery working range. If the battery goe beyonds the set limit it gives pop-ups every 10 seconds. No sound warning as sound can easily be muted and ignored.
# Things that could be added : 1> The flexibilty of range. But it is not recommended  hence not implemented. 2> Notification in form of email or sms. 3> Sound / beep for critical state of battery.
# The script goes into background. To kill it at any time use : kill `pgrep battery-notify`

# check if "acpi" and "notify-send" is installed.

if [ ! `which acpi` ] ; then
echo -e "Install acpi  \nsudo apt-get install acpi"
exit 0

if [ ! `which notify-send` ] ;then
echo -e "Install notify-send \nsudo apt-get install libnotify-bin"
exit 0

v1=`echo $0 | sed 's/\.//' | sed 's|\/||' ` 

echo -e "The script goes into background. To kill it at any time use : kill \`pgrep $v1\`" 

while [ 1 ]
# this snippet extracts the battery status as percentage
aa=`acpi | cut -d"," -f2 | tr -d "% "`
# grep -c counts the no. of occurance of the text
bb1=`acpi | grep -c "Discharging"`
bb2=`acpi | grep -c "Charging"`
#echo $bb1 $bb2

# if discharging then
if [[ $bb1 > 0 ]] ; then
if [[ $aa < 20 ]] ; then 
 notify-send -t 100 "Battery Status: $aa%. Plug charger" 

# if charging then
if [[ $bb2 > 0 ]] ; then
if [[ $aa > 80 ]] ; then 
 notify-send -t 1000 "Battery Status: $aa%. Unplug charger." 

sleep 10



No comments:

OK GOOGLE on Samsung Galaxy S7 doesn’t work

To make Ok Google detection work on Galaxy S7 (Galaxy series phones) we need to perform a couple of steps. 1. As long as Samsung S vo...