If I understood your question correctly, you want to check the battery status in every X interval of time. It's easiest way that you run it in the background, inside a while loop:
while true
do
# Any script or command
sleep [number of seconds]
done
And if you want to run the script on startup and from then on every 5 minutes, so the construction would be:
- Add a line in
/etc/rc.local
to call your script (your battery_status.sh
) + "&" to make it exit.
- Add the commands you want to run in the
battery_status.sh
to execute in a while loop (inside battery_status.sh).
Note that if you want to run it from cron, you need to set the full path, since cron
runs with a limited set of environment variables.
Example
#!/bin/bash
while true
do
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [ $battery_level -ge 60 ]; then
notify-send "Battery is above 60%!" "Charging: ${battery_level}%"
elif [ $battery_level -le 40 ]; then
notify-send "Battery is lower 40%!" "Charging: ${battery_level}%"
fi
sleep 300 # 300 seconds or 5 minutes
done
Save this file by named battery_status.sh
in your favorite location (mine is home
directory) and add this line in /etc/rc.local
file (in my example, just replace your battery_status.sh location by /home/username/
):
sh /home/username/battery_status.sh &
That's all. Reboot and see the magic.
If you don't have instell acpi
, just install that using sudo apt-get install acpi
One BONUS
If you want to run make this script responsible with AC adapter,you don't need an extra variable to check that run for one time. If your AC adapter is plugged and battery charged above 60% it makes a alert to "Unplug your adapter!" until you don't unplug that. If alert tell you

just unplug AC adapter then you will see the message alert don't appear again until your battery charge down to 40%. Then another message alert and tell you

if you don't unplug AC adapter on above 60% or don't plug AC adapter below 40%, the alert message shown every 5 minutes (you can adjust that for yourself in code, see sleep [seconds]
) show and Will remind you.
#!/bin/bash
while true
do
export DISPLAY=:0.0
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if on_ac_power; then
if [ $battery_level -ge 60 ]; then
notify-send "Battery charging above 60%. Please unplug your AC adapter!" "Charging: ${battery_level}% "
sleep 20
if on_ac_power; then
gnome-screensaver-command -l ## lock the screen if you don't unplug AC adapter after 20 seconds
fi
fi
else
if [ $battery_level -le 40 ]; then
notify-send "Battery is lower 40%. Need to charging! Please plug your AC adapter." "Charging: ${battery_level}%"
sleep 20
if ! on_ac_power; then
gnome-screensaver-command -l ## lock the screen if you don't plug AC adapter after 20 seconds
fi
fi
fi
sleep 300 # 300 seconds or 5 minutes
done
" ./battery-monitor.sh: line 8: [: too many arguments"
OS: Ubuntu 20.04
– Aneeez Jan 18 '21 at 05:19Battery 0: Discharging, 94%, 09:19:19 remaining Battery 1: Discharging, 0%, rate information unavailable
So, acpi -b | grep -P -o '[0-9]+(?=%)' would give the output 93 0. I think that's the problem. Is there anyway I can filter out only battery 0? (I don't know why this command shows 2 batteries)
– Aneeez Jan 18 '21 at 06:33