CentOS 7.x laptop low battery alerts

From Notes_Wiki
Revision as of 06:44, 12 June 2017 by Saurabh (talk | contribs) (Created page with "<yambe:breadcrumb>CentOS_7.x_laptop_battery|CentOS 7.x laptop battery</yambe:breadcrumb> =CentOS 7.x laptop low battery alerts= Create '<tt>~/bin/notify-send_setup</tt>' with...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

<yambe:breadcrumb>CentOS_7.x_laptop_battery|CentOS 7.x laptop battery</yambe:breadcrumb>

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:


<yambe:breadcrumb>CentOS_7.x_laptop_battery|CentOS 7.x laptop battery</yambe:breadcrumb>