-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbattery-watch.sh
More file actions
36 lines (30 loc) · 980 Bytes
/
Copy pathbattery-watch.sh
File metadata and controls
36 lines (30 loc) · 980 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# battery-watch.sh
# Continuously monitors battery levels and sends notifications
# Works with acpi and notify-send
# CONFIGURATION
LOW=15 # Battery percentage considered low
CRITICAL=5 # Battery percentage considered critical
INTERVAL=120 # Check interval in seconds
# Ensure acpi is installed
if ! command -v acpi &>/dev/null; then
echo "Error: acpi is not installed. Please install it."
exit 1
fi
# Infinite loop
while true; do
STATUS=$(acpi -b)
if [[ "$STATUS" != *"Discharging"* ]]; then
sleep $INTERVAL
continue
fi
# Extract battery percentage as a number
PERCENT=$(echo "$STATUS" | grep -oP '\d+%' | tr -d '%')
# Send notifications
if (( PERCENT <= CRITICAL )); then
notify-send -u critical "Battery Critical" "Battery at ${PERCENT}%. Plug in now."
elif (( PERCENT <= LOW )); then
notify-send -u normal "Battery Low" "Battery at ${PERCENT}%."
fi
sleep $INTERVAL
done