You can use this script as I was stuck today on the same problem.
You can think of this script as an upgrade to the script given by @Danibix.
The script below uses systemd service as to restart itself even if the process is killed.
#!/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%. Also you can create a systemd service.
# Cronjob line example:
# */5 * * * * /bin/bash /path/to/battery_health_notifications.sh
export $(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep -u $LOGNAME gnome-session | head -n 1)/environ | tr '\0' '\n')
export XDG_RUNTIME_DIR="/run/user/1000"
while ((1)) ; do
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 75 ]]; then
notify-send --hint=int:transient:1 --urgency=critical "Battery optimization" "Battery reached 75%, unplug the power cable to optimize battery life."
#Using my own alarm file. You can choose your desired alarm file.
/bin/paplay /home/ali/.local/alarm.wav
fi
else
if [[ $BATTERY_PERCENTAGE -lt 30 ]]; then
notify-send --hint=int:transient:1 --urgency=critical "Battery optimization" "Battery is below 30%, plug in the power cable to optimize battery life."
#Using my own alarm file. You can choose your desired alarm file.
/bin/paplay /home/ali/.local/alarm.wav
fi
fi
sleep 2
done
Put the above script anywhere in your home folder as battery_health_notifications.sh
After that create a service file as battery_alarm.service and copy the text below in it.
Make sure you read the comments in the service file below to configure it according to your system.
[Unit]
#just what it does
Description= Battery alarm notifies the user about charge and discharge cycle of battery
[Service]
#not run by root, but by me. Change User to your username
User=ali
#we assume the full service as active one the script was started
Type=simple
#where to find the executable
ExecStart=/home/ali/.local/battery_health_notifications.sh
#what you want: make sure it always is running
Restart=always
[Install]
#which service wants this to run - default.target is just it is loaded by default
WantedBy=default.target
Open the terminal where your battery_alarm.service is located and copy the commands below onto your terminal.
sudo mv battery_alarm.service /etc/systemd/system/
systemctl enable battery_alarm.service
systemctl start battery_alarm.service
Your battery alarm setup is complete. To check if its running or not type the below command onto your terminal.
systemctl status battery_alarm.service
You would get something like this.
● battery_alarm.service - Battery alarm notifies user about charge discharge cycle of battery
Loaded: loaded (/etc/systemd/system/battery_alarm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2020-05-16 20:26:34 PKT; 23min ago
Main PID: 166450 (battery_health_)
Tasks: 2 (limit: 12979)
Memory: 2.6M
CGroup: /system.slice/battery_alarm.service
├─166450 /bin/bash /home/ali/.local/battery_health_notifications.sh
└─183382 sleep 2
May 16 20:26:34 pop-os systemd[1]: Started Battery alarm notifies the user about charge-discharge cycle of the battery.