Skip to content

Instantly share code, notes, and snippets.

@bandaangosta
Created October 20, 2018 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bandaangosta/0a0fdbd03215b925576c4b234fc4d4f1 to your computer and use it in GitHub Desktop.
Save bandaangosta/0a0fdbd03215b925576c4b234fc4d4f1 to your computer and use it in GitHub Desktop.
Bash script for low battery alarm and notification
#!/bin/bash
# From: https://forums.linuxmint.com/viewtopic.php?f=90&t=217741
# Credits to user BlackVeils
# Steps:
# 1. Save this script to battery_notify.sh
# 2. Edit the 'alarm' variable to point to your sound file of choice
# 3. Give execution permission: chmod +x battery_notify.sh
# 4. Run the script to test it. If happy, add to your startup applications.
# Set the alert tone for battery, or leave it as empty quotes.
# .mp3 or .ogg audio file
alarm="/your/path/battery_notification.ogg"
# Notify when below this percentage
warning_level=21
# How often to check battery status, in minutes
check_interval=2
while true; do
battery_level=$(acpi -b \
| cut -d, -f2 | cut --characters=2,3,4 \
| sed 's/%//g')
charging=$(acpi -b | grep -c "Charging")
# When battery is low, and not already charging
if [ $battery_level -lt $warning_level ] &&
[ $charging -eq 0 ]
then
play -q -v 0.40 "$alarm" &
notify-send " Low battery: ${battery_level}% " \
" Plug into mains power " -t 8000
fi
sleep ${check_interval}m
done
@bandaangosta
Copy link
Author

I forgot to mention that you need the acpi application to be installed. On Debian:
sudo apt-get install acpi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment