-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery_monitor
executable file
·49 lines (42 loc) · 1.36 KB
/
battery_monitor
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
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/bash
get_battery_level() {
# Initialize variables
total_battery_level=0
# If any batteries exist, enter loop
if compgen -G "/sys/class/power_supply/BAT*" > /dev/null; then
# Iterate over all of the batteries
for battery_path in "/sys/class/power_supply/BAT"*; do
# Get battery information
battery_level=$(cat "${battery_path}/capacity")
total_battery_level=$(( battery_level + total_battery_level ))
# Return the battery level
echo "${total_battery_level}"
done
fi
}
get_ac_status(){
if ! [[ $(cat "/sys/class/power_supply/A"[CD]*"/online") -ge '1' ]]; then
echo 'Discharging'
else
echo 'Charging'
fi
}
battery_level="$(get_battery_level)"
ac_status="$(get_ac_status)"
export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/${UID}/bus"
export DISPLAY=':0'
display_ids=()
for display_proc in $(for proc_file in /proc/[0-9]*; do
grep -ao 'DISPLAY=[^[:cntrl:]]*' "${proc_file}/environ" 2>/dev/null
done|sort|uniq|awk -F'=' '{print $2}'); do
display_ids+=("${display_proc}")
done
for display_id in "${display_ids[@]}"; do
export DISPLAY="${display_id}"
if [[ "${ac_status}" == 'Discharging' && "${battery_level}" -lt 30 ]]; then
notify-send -t 59000 -u critical \
-a "battery_monitor" \
"Battery is Low." \
"Level: $battery_level %"
fi
done