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?

6
  • just out of curiosity what's the rationale behind those numbers? Commented Dec 19, 2014 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 Commented Dec 19, 2014 at 18:09
  • 1
    @1_CR - the motivation probably comes from an article such as this one Commented Dec 19, 2014 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/… Commented Dec 19, 2014 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. Commented Dec 19, 2014 at 19:40

1 Answer 1

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
3
  • See Why $(..) is preferred over backticks, the difference between [ and [[ and sparing the cat with high=$(</sys/class... Commented Dec 23, 2014 at 4:41
  • @jasonwryan You can edit the answer. Then Thanks for the information. Commented Dec 23, 2014 at 4:51
  • It would be a pretty major rewrite: I'll leave it to you if you are interested... Commented Dec 23, 2014 at 5:30

You must log in to answer this question.