CentOS 7.x laptop low battery alerts

From Notes_Wiki

Home > CentOS > CentOS 7.x > Monitoring > Laptop battery > CentOS 7.x laptop low battery alerts

Create '~/bin/notify-send_setup' with

#!/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


Create '~/bin/battery-check' with

#!/bin/bash
export DISPLAY=:0

XAUTHORITY=/home/saurabh/.Xauthority

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


#BAT='upower -i /org/freedesktop/UPower/devices/battery_BAT0| grep -E "state|to\ full|percentage"'
STATE=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'state' | sed 's/ *state: *//g' )
PERCENTAGE2=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep 'percentage' | sed 's/ *percentage: *//g' )
PERCENTAGE=$(echo $PERCENTAGE2 | sed 's/\%//g')


if ( [[ "$STATE" != "charging" ]] &&  (( "$PERCENTAGE" < "35" )) ); then
	notify-send -u critical "Battery low" "Battery level is ${PERCENTAGE}%!"
fi


Then edit crontab for user using "crontab -e" and add

*/5 * * * * sh /home/saurabh/bin/battery-check

Replace saurabh with your username appropriately. It is important to run this with current user.


Then /home/saurabh/bin/notify-send_setup must be executed once for every graphical login for things to work, so that ~/.dbus/Xdbus is configured correctly.


Refer:


Home > CentOS > CentOS 7.x > Monitoring > Laptop battery > CentOS 7.x laptop low battery alerts