Bash Script to get Low Battery Alert in Linux
We are going to write a bash script that will speak “Battery Low” when the battery’s charging percentage will go below some specified threshold value. Before starting this, first, we need to understand some basic commands and terminology which are described below:
Some Bash Commands:
- acpi: This command returns the percentage of battery charged, whether the device is charging or discharging and the amount of time left before the battery fully discharges.

Example: acpi -b produces the following output:
Battery 0: Discharging, 13%, 00:52:52 remaining
Here, Battery 0 means it is not charging and Battery 1 means it is charging. 13% is the Percentage of Battery Charged. 00:52:52 is the time left before it Hibernates.
- notify-send: This command is used to alert the notification popups in Linux. We will have the alert of the battery charging/discharging warning with this command.

Required Package: espeak command speaks or narrates whatever parameter you pass with it e.g it will speak out “demo espeak” if you run espeak “demo espeak”. Espeak can be installed like any normal package:
sudo apt install espeak
Required Linux Filters:
- grep: The grep filter is used to search for some specified string in a given file.
- cut: The cut filter allows us to cut and strip the desired section of text file before the standard output.
- sed: It stands for stream editor. This filter is used to make insertion, deletion, search, and replacement. We are using sed here for replacing the “%” sign by “”.
- Bash shell script to find sum of digits
- Bash shell script to swap two numbers
- Write a bash script to print a particular line from a file
- Useful and time saving bash commands in Linux
- script command in Linux with Examples
- How to Install Lazy Script in Kali Linux?
- What is the need of CMOS battery in Computers?
- Average of given numbers in Bash
- Simple Calculator in Bash
- Fibonacci Series in Bash
- How To Use Bash Shell Natively On Windows 10
- Programs for printing different patterns in Bash
- Bash program to find A to the power B
- Bash program to check if the Number is a Palindrome
- Sorting an array in Bash using Bubble sort
#!/bin/bash #Author:echo_bash | Ali Anwar battery_level=$(acpi -b |cut -d ", " -f2| sed 's/%//g') #Battery 0: Discharging, 13%, 00:52:52 remaining #To get the battery percentage only we'll cut the #second value which ends at ", ". So we get 13% #Now we'll replace the % sign by "", so 13% will #be changed to 13 now. #acpi -b produces output as echo $battery_level #$battery_level=13 #If the charger is plugged in, acpi shows "charging" #and if it's not plugged in, it shows "discharging". #if acpi -b shows charging, "grep -c" will return 1 #else it will return 0 ac_power=$(acpi -b|grep -c "Charging") echo $ac_power #1 if charging(plugged in) and 0 if discharging (not plugged in) #when the battery is charging and it gets charged up to 100% if [[ $ac_power -eq 1 && $battery_level -eq 100 ]] #if charging and battery_level==100 thenexport DISPLAY=:0.0 notify-send -i "/usr/local/bin/battery_full.png" "Ali, Battery is full." "Level: $battery_level% "; #it notifies Ali, Battery is full and shows the battery #full image which is stored in /usr/local/bin directory #so, it will narrate, please remove the #charger. It's charged up to 100%. espeak "Charge full, Please Remove the charger" -s 140 fi#when the battery is not charging #and it goes below 30% if [[ $ac_power -eq 0 && $battery_level -lt 30 ]] thenexport DISPLAY=:0.0 notify-send -i "/usr/local/bin/battery_low.png" "Ali, Battery is Low." "Level: $battery_level% "; #similarily, it narrates #"please connect the charger" espeak "Please connect the charger" -s 140 fi |
How to run this script?
You can set a cron job to run this script every hour. So, the script will check battery percentage and charging/discharging every hour and will narrate the result.
Recommended Posts:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



