Battery level checking service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Save this file as: /etc/systemd/system/battery-level-check.service | |
# | |
[Unit] | |
Description=Battery check. If higher than 80% or lower than 40%, notify desktop user | |
Wants=battery-level-check.timer | |
[Service] | |
Type=simple | |
ExecStart=/usr/local/bin/battery-level-check | |
[Install] | |
WantedBy=graphical.target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Save this file as: /usr/local/bin/battery-level-check | |
# | |
function notify_user { | |
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS sudo -u $DESKTOP_USER -E notify-send \ | |
--urgency=critical \ | |
$@ | |
} | |
IFS=\0 | |
# Assuming the system has tty2 dedicated for desktop user | |
DESKTOP_USER=$(who | grep tty2 | cut -d' ' -f1) | |
if [ "$DESKTOP_USER" == "" ]; then | |
# no active desktop user | |
exit 0 | |
fi | |
# Gather desktop user and power information | |
DESKTOP_UID=$(id -u "$DESKTOP_USER") | |
DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$DESKTOP_UID/bus" | |
POWER=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep percentage | cut -d':' -f2 | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//') | |
POWER_NUM=${POWER%\%} | |
BATTERY_STATE=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep state | cut -d':' -f2 | sed 's/^[[:blank:]]*//;s/[[:blank:]]*$//') | |
BATTERY_ICON=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep icon-name | cut -d':' -f2 | sed 's/^[[:blank:]]*'"'"'//;s/'"'"'[[:blank:]]*$//') | |
# Check battery level | |
if [ $POWER_NUM -le 40 ] && [ "$BATTERY_STATE" == "discharging" ]; then | |
notify_user \ | |
--icon="$BATTERY_ICON" \ | |
"Low Battery" \ | |
"Too few battery: $POWER. Start charging it up." | |
elif [ $POWER_NUM -gt 80 ] && [ "$BATTERY_STATE" == "charging" ]; then | |
notify_user \ | |
--icon="$BATTERY_ICON" \ | |
"Battery Level High" \ | |
"High battery level: $POWER. Stop charging." | |
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Save this file as: /etc/systemd/system/battery-level-check.timer | |
# | |
[Unit] | |
Description=Battery check. If higher than 80% or lower than 40%, notify desktop user | |
Requires=battery-level-check.service | |
[Timer] | |
Unit=battery-level-check.service | |
# Runs every 5 minutes | |
OnCalendar=*:0/15 | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment