Created
March 10, 2025 13:45
-
-
Save ostechnix/191d2d1567bffa14c9fd5da090145dba to your computer and use it in GitHub Desktop.
battmon: A Simple Bash Script for Battery Charge Notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notification for Laptop Battery Fully Charged:
Low Battery Notification: