How to Remind Unplug a Charging Laptop on Arch Linux

You are always worried about over-charging your laptop when too busy on projects that you can't keep checking battery percentage?. It is always recommended to keep battery power between 40% and 80% to prolong its lifespan. There is no sure solution available to help you automatically disconnect a charger when a battery is fully charged or connect it when too low.

In this guide, I'll share with you a bash script which should notify you whenever the battery charge reaches 80% while plugged into a charger and when it drops to 40% mark while discharging. This should help you not to overcharge your battery or drain it to 0%. I'm doing this setup on Arch Linux machine, but it should work fine with any other Linux distribution.

Install dependencies (Only for Arch Linux)

The only package required by this is libnotify which provides a notify-send command. The notify-send is a program to send desktop notifications to the user via a notification daemon from the command line. These notifications can be used to inform the user about an event or display some form of information without getting in the user's way.

This is installed on Arch Linux using the command.

$ sudo pacman -S libnotify

If you're running a Desktop environment, it should come with notify-send command, so you don't need to install the package.

Battery check and notification script

This is the script used to check battery percentage and notify.

$ cat batry_notify.sh
#! /bin/bash
set -eu
MIN_BAT=40
MAX_BAT=80
UNPLUGGED=`cat /sys/bus/acpi/drivers/battery/*/power_supply/BAT?/status|grep -i discharging`
BAT_PERCENTAGE=`acpi|grep -Po "[0-9]+(?=%)"`

while true ; do

if [ $BAT_PERCENTAGE -le $MIN_BAT ]; then # Battery under low limit
 notify-send "Battery under $MIN_BAT. Please plug in the adapter"

elif [ $BAT_PERCENTAGE -ge $MAX_BAT ]; then # Battery over high limit
 if [ "$UNPLUGGED" == "" ]; then # plugged
 notify-send "Battery above $MAX_BAT. Please remove the adapter"
 fi
 fi
 sleep 10 #Repeat every 10 seconds

done

What the script does

This script will keep checking for battery power percentage. If the current percentage is lower than set minimum and it's unplugged from power, a notification message will be printed on the screen. Same as when it's charging and it hits set maximum power value. You can adjust the values to your liking.

Using the script

Make the script executable by running:

$ chmod +x battery_notify.sh

To execute the script, run the command:

$ ./battery_notify.sh

I'll recommend you copy the script to a directory in your $PATH, e.g /usr/local/bin

$ sudo cp battery_notify.sh /usr/local/bin/battery_notify

This way, whenever you want to run the script, just type

$ battery_notify

For demonstration purposes, I modified the variables to have MIN_BAT=66. This is because my current battery value is 65%. If I run the script it should see a popup notification like below.

This script is simple and doesn't have fancy colors and icons to make notification look cooler. Feel free to modify/improve it and share.

Josphat Mutai 12:34 am

About Josphat Mutai

Hat Certified Engineer with more than 2 years experience managing critical Cloud Infrastructures and Services. Passionate about Technology and loves testing and reviewing gadgets and Open-source tools.

Author Archive Page

Have anything to say?

Your email address will not be published. Required fields are marked *

All comments are subject to moderation.