Skip to content

Instantly share code, notes, and snippets.

@ostechnix
Created March 10, 2025 13:45
Show Gist options
  • Save ostechnix/191d2d1567bffa14c9fd5da090145dba to your computer and use it in GitHub Desktop.
Save ostechnix/191d2d1567bffa14c9fd5da090145dba to your computer and use it in GitHub Desktop.
battmon: A Simple Bash Script for Battery Charge Notifications
#!/usr/bin/env bash
# ------------------------------------------------------------------
# Script Name: battmon.sh
# Description: A Simple Bash Script for Battery Level Charge
# Notifications
# Website: https://gist.github.com/ostechnix
# ------------------------------------------------------------------
# Define thresholds
HIGH_THRESHOLD=95
LOW_THRESHOLD=10
LOGFILE="/tmp/battmon.log"
# Get the battery level
LEVEL=$(acpi -b | awk -F', ' '{print $2}' | tr -d '%,')
# Ensure LEVEL is a valid number
if [[ "$LEVEL" =~ ^[0-9]+$ ]]; then
# Check for high battery level
if [ "$LEVEL" -ge "$HIGH_THRESHOLD" ]; then
echo "$(date) - Battery at $LEVEL%. Sending high battery notification..." >> "$LOGFILE"
DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/1000 notify-send -t 0 "Battery Full" "Your battery is now fully charged." >> "$LOGFILE" 2>&1
fi
# Check for low battery level
if [ "$LEVEL" -le "$LOW_THRESHOLD" ]; then
echo "$(date) - Battery at $LEVEL%. Sending low battery warning..." >> "$LOGFILE"
DISPLAY=:0 XDG_RUNTIME_DIR=/run/user/1000 notify-send -t 0 "Battery Low" "Your battery is critically low. Please plug in your charger!" >> "$LOGFIL>
fi
fi
@ostechnix
Copy link
Author

Notification for Laptop Battery Fully Charged:

Monitor Battery Levels on Linux with Notifications using battmon

Low Battery Notification:

Set Up Laptop Battery Alerts Using battmon

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