4

I want to plug in the laptop battery charger when the battery level goes down to 40% and then plug it out when the battery charge level reaches 80%. And for that reason, I need a script that would remind me to plug in the charger when the battery level is 40% and again would remind me when the battery charge level reaches 80%. How would be the script? Could anything else do that?

  • just out of curiosity what's the rationale behind those numbers? – iruvar Dec 19 '14 at 17:49
  • 1
    Take a look in /sys/class/power_supply/BAT0/. Or if you're running upower, then upower -i /org/freedesktop/UPower/devices/battery_BAT0 – derobert Dec 19 '14 at 18:09
  • 1
    @1_CR - the motivation probably comes from an article such as this one – jayhendren Dec 19 '14 at 18:24
  • 1
    @1_CR Having seen a prolonged brawl on this topic before, I'm pretty confident that it is true, the extreme end of charge/discharge shortens the (lithium ion) battery life, and apparently 70% is the ideal level (i.e., if you are going to store it for a while, leave it at that). batteryuniversity.com/learn/article/… – goldilocks Dec 19 '14 at 18:29
  • 2
    If you are using good shell, like zsh, then you can put battery charge level into right prompt with RPS1=$(acpi|grep -o "[0-9]*%")%. Then you can put red color on it if value is lower then 40 or higher then 80 etc. – jimmij Dec 19 '14 at 19:40
1

Try this one. In my ubuntu 12.04 it is working perfectly.

#!/bin/bash
high=$(cat /sys/class/power_supply/BAT0/charge_full_design)
now=$(cat /sys/class/power_supply/BAT0/charge_now)
stat=$(cat /sys/class/power_supply/BAT0/status)
echo -e "scale=1\n$now/$high * 100\nquit"> hi
per=$(bc hi)
per=$(expr "$per" : '\(.*\)\..*')

if [ $stat == Charging ] ; then
    if [ $per -gt 80 ] ; then
            zenity --warning --text="BATTERY IS FULL REMOVE THE CHARGER"
    fi
elif [ $stat == Discharging ] ; then
    if [ $per -lt 40 ] ; then
            zenity --warning --text="BATTERY IS LOW PLUGIN THE CHARGER"

    fi
fi
rm hi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy