50

This happens to me more than I'd like to admit, but sometimes I don't notice the power getting low since it only shows up in my status bar, then the computer just quits. I'd like a big alert that warns me before this happens. Is there any way for it to alert me? Ubuntu has a nice popup that tells you it's getting low.

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255

13 Answers13

37

write a script!

battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [ $battery_level -le 10 ]
then
    notify-send "Battery low" "Battery level is ${battery_level}%!"
fi

then cron it to run every few minutes or so. But yeah, if you can do it through the GUI, that's probably a much better way of doing it.

hdgarrood
  • 471
  • This method has the advantage that it will apply to just about any distro/desktop environment combination with a little tweaking. – Perkins Oct 22 '15 at 16:53
  • The best way I've found to 'do it through the GUI' in my Cinnamon environment is to add an applet to a panel: http://unix.stackexchange.com/a/353505/37552 – Michael Scheper Mar 24 '17 at 06:59
9

re: hdgarrood 's answer, setting cron to run notify-send actually turned out to be painfully finicky. (I think crontab -e does persist across reboots by default, fortunately). I followed several guides, I'm not sure what exactly fixed it in the end, but here is my full setup for running the checker every 5 minutes:

$ crontab -e

*/5 * * * * sh /home/nrw/.notify-send_setup
*/5 * * * * sh /home/nrw/.battnotif

$ cat .notify-send_setup

#!/bin/bash
touch $HOME/.dbus/Xdbus
chmod 600 $HOME/.dbus/Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

exit 0

$ cat .battnotif

#!/bin/bash
export DISPLAY=:0
XAUTHORITY=/home/nrw/.Xauthority

if [ -r "$HOME/.dbus/Xdbus" ]; then
    . "$HOME/.dbus/Xdbus"
fi

battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`

# I tried to only notify when not charging, but could not get it to work
# STATUS=$(cat /sys/class/power_supply/ADP1/online)
# if [ $battery_level -le 15 ] && [ $STATUS == "0" ]

if [ $battery_level -le 15 ]
then
    /usr/bin/notify-send -u critical "Battery low" "Battery level is ${battery_level}%!"
    echo 'batt low' >> /home/nrw/cron.log
fi

echo 'ran batt' >> /home/nrw/cron.log

Make sure to chmod +x the bash scripts.

9

With the help of all the information from all the answers here I created a script and put it in a repository on GitLab.

It'll show notifications when:

  • Your cable is unplugged and the battery goes below 30%
  • Your cable is plugged and the battery goes above 80%

https://gitlab.com/gitaarik/battery-health-notifications

Contributions to make it work on a wider range of environments are very welcome.

The script at time of writing this:

#!/bin/bash

Run this script as a cronjob every 5 minutes or so, to get notifications when

battery percentage goes below 30% or above 80%.

Cronjob line example:

/5 * * * /bin/bash /path/to/battery_health_notifications.sh

This line is to make notify-send always work, also when run in a crontab.

https://askubuntu.com/questions/298608/notify-send-doesnt-work-from-crontab/346580#346580

export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ | tr '\0' '\n')

BATTERY_PATH=$(upower -e | grep battery) LINE_POWER_PATH=$(upower -e | grep line_power) BATTERY_PERCENTAGE=$(upower -i $BATTERY_PATH | grep 'percentage:' | awk '{ print $2 }' | sed 's/%//') CABLE_PLUGGED=$(upower -i $LINE_POWER_PATH | grep -A2 'line-power' | grep online | awk '{ print $2 }')

if [[ $CABLE_PLUGGED == 'yes' ]]; then

if [[ $BATTERY_PERCENTAGE -gt 80 ]]; then
    notify-send --urgency=critical "Battery optimization" "Battery reached 80%, unplug the power cable to optimize battery life."
fi

else

if [[ $BATTERY_PERCENTAGE -lt 30 ]]; then
    notify-send --urgency=critical "Battery optimization" "Battery is below 30%, plug in the power cable to optimize battery life."
fi

fi

gitaarik
  • 559
6

I do now run the following:

$ crontab -e
*/5 * * * * /home/<my username>/bin/checkLowBattery
$ cat /home/<my username>/bin/checkLowBattery
#!/bin/bash

POWERSUPPLY="/sys/class/power_supply/ACAD/online" # could be different on your system!
TOO_LOW=20 # how low is too low?
NOT_CHARGING="0"
ICON="/usr/share/icons/ubuntu-mono-dark/status/24/battery-low.svg" # eye candy

export DISPLAY=:0

BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+(?=%)')
STATUS=$(cat $POWERSUPPLY)

if [ $BATTERY_LEVEL -le $TOO_LOW -a $STATUS = $NOT_CHARGING ]
then
    /usr/bin/notify-send -u critical -i "$ICON" -t 3000 "Battery low" "Battery level is ${BATTERY_LEVEL}%!"
fi

exit 0

As you can see this is just orangenarwhals code with some changes:

  • no dbus trickery, somehow I didn't need it
  • put some "constants" into variables at the beginning of code
  • implemented warnings only when battery is not charging (take a look at man test, it explains how to do ANDs, among other things.)
  • added icon (find some candidates on your system with something like: $ find /usr/share/icons/ -iname "*batt*low*")
  • expiry-time in milliseconds

don't forget to chmod +x your scripts, you can then run them directly from cron(tab), no need for sh.

(This works on Ubuntu using Xmonad.)

HalosGhost
  • 4,790
Higemaru
  • 659
6

Since you're apparently running Cinnamon, just install Battery Applet with Monitoring and Shutdown (BAMS).

Right-click on a panel → + Add Applets to the PanelAvailable applets (online), type 'BAMS' in the search widget, and install and configure it.

It's not the flashiest, but so far it's already alerted me when I had my laptop unwittingly unplugged.

2

Minor twist - on Knoppix (granted, not Mint as used by OP) notify-send wasn't available (libnotify-bin package is not installed), so instead I found this useful for the alert:

xmessage Battery low & mplayer /usr/lib/libreoffice/share/gallery/sounds/beam.wav

I just threw that into the Alarm Command of the Battery Monitor panel of LXDE, no need for a script.

That assumes you have LibreOffice installed, but that .wav makes a nice low power sound. There are practically no .ogg's on Knoppix.

Randall
  • 445
2

This simple one works nicely for me, with just simple acpi package needed to be installed (no deamons, specific desktop enviroments or the like).

In your user crontab (crontab -e) put the following (all in one line, it is split for readability here):

*/3 * * * * acpi --battery | 
    awk -F, '/Discharging/ { if (int($2) < 15) print }' | xargs -ri 
    env DISPLAY=:0 zenity --warning --text "battery low\n{}"

What it does is check battery level (acpi --battery) and if you're not connected to charger (/Discharging/) and if battery is < 15%, it will every 3 minutes (*/3) display message via zenity to bug you.

Instead of zenity you could instead use xmessage (on really spartan machine) or even something like aplay -q /usr/lib/libreoffice/share/gallery/sounds/cow.wav for audio notifications (which have advantage that it will won't interrupt your keyboard typing, and will work even for multi-user laptops with multiple sessions, where DISPLAY=:0 hack would fail)

If you also need to suspend/hibernate machine on certain critical level, see this answer

Matija Nalis
  • 3,111
  • 1
  • 14
  • 27
1

I had that same problem and I figured out a workaround. I installed from Linux Mint repositories an app called GKrellM. It monitors lots of stuff on your machine and there is battery. Setup with alarms, it works for me like a charm.

more info at:

http://www.gkrellm.net/

slm
  • 369,824
1

The issue is related at KDE environment. I have the same problem with MintMAYA KDE_version. When I worked with Mint9_Gnome a very useful pop-up appeared when a certain level of low-power was reached (auto-configured out_of_the_box on fresh installs). Very very annoying being shutted down without advice and no help founded even googling a lot :(

Symb932
  • 365
1

I have the same issue and it's really frustrating. I solved by using the BatteryAlert.sh script:

#!/bin/bash

BATTERY=$(upower -e | grep 'BAT')

while :
do
    BATTERY_PERCENTAGE=$(upower -i $BATTERY|grep percentage|awk '{ print $2 }'|sed s/'%'/''/g)
    CABLE=$(upower -d | grep -n2 line-power | grep online | awk '{ print $3 }')

    if [[ "$BATTERY_PERCENTAGE" -lt "10" && $CABLE = "no" ]]; then

        notify-send --urgency=critical "WARNING: Battery is about to die"  "Plug in the power cable"
        play /usr/share/sounds/KDE-Sys-Warning.ogg &> /dev/null 
    fi

sleep 10

done

I'm using KDE but you can use it with every desktop environment. You don't have to run this as root user or go crazy with crontab syntax. You need only to change the notification sound if you want a sound alert. If you wan't to start the script automatically when the desktop environment starts put the script in $USER/bin folder with the BatteryAlertLauncher.sh script:

#!/bin/bash

function getRunningProcesses 
{
    ps -aux | grep -v grep | grep BatteryAlert.sh
}

if [[ -n "$(getRunningProcesses)" ]] ; then
    exit
fi

BatteryAlert.sh&

Make sure that they are executable:

chmod u+x $USER/bin/BatteryAlert*

Then just run the following command:

ln -s /home/$(USER)/bin/BatteryAlertLauncher.sh .config/autostart/

You can also run the alert script manually from bash by entering this command:

BatteryAlertLauncher.sh
L29Ah
  • 823
Bemipefe
  • 165
0

This is how I did it on my Arch linux with Gnome as DE:

#!/bin/bash

eval "export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session)/environ)";
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`

STATUS=$(cat /sys/class/power_supply/ACAD/online)
if [ $battery_level -le 15 ] && [ $STATUS == "0" ]
then
    /usr/bin/notify-send -u critical "Battery low" "Battery level is ${battery_level}%!"
fi

The word ACAD in STATUS=$(cat /sys/class/power_supply/ACAD/online) will change depending on the laptop make. It is used to find if your lap is plugged in to a AC ADapter or not. Here is how my crontab looks like:

* * * * * bash <path to script>

Don't forget to make the script executable with chmod u+x <path to script>

0

Download an applet called BAMS . It will alert you automatically when the battery reaches a certain level decided by you . It is the simplest solution.

0

As I don't have Gnome Shell or Cinnamon installed, I cannot be of direct help as accessing the source could would be much practical since it's rather easy adding such a rudimentary extension with JavaScript.

You should open :

/usr/share/cinnamon/js/ui/status/power.js

and search for percentageText - subsequently, you ought to be able to add a pop-up message or alert.