-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0495bfc
commit 0033150
Showing
1 changed file
with
34 additions
and
24 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,46 @@ | ||
#!/bin/bash | ||
# script that warns the user if battery | ||
# capacity is less than 20 and less than 10 | ||
# this runs with cron (see "man crontab") | ||
|
||
# get a list of batteries | ||
for battery in /sys/class/power_supply/BAT? | ||
do | ||
# get various information: capacity (%), status (charging/discharging/unknown), battery name. | ||
# get capacity | ||
# Warn the user if the battery capacity is low | ||
# Run this script with cron (see "man crontab") | ||
|
||
# Make a beep sound at 1000Hz for 0.25sec | ||
function makebeep() { | ||
speaker-test -t sine -f 1000 >/dev/null 2>&1 & pid=$!; sleep 0.25s; kill -9 $pid | ||
} | ||
|
||
# Get a list of batteries | ||
for battery in /sys/class/power_supply/BAT?*; do | ||
# Get battery capacity | ||
capacity=$(cat "$battery"/capacity) | ||
|
||
# this transforms a number like "9" to "09" | ||
# because bash fails if there is a statement checking | ||
# if 9 < 10, but works if 09 < 10 | ||
# Transform a number like "9" to "09", because bash fails if | ||
# there is a statement checking if 9 < 10, but works if 09 < 10 | ||
[[ $capacity =~ ^[0-9]$ ]] && capacity="0${capacity}" | ||
|
||
# get the current battery status (charging/discharging) | ||
# Avoid sending unnecessary notifications | ||
[[ $capacity > 20 ]] && exit 0 | ||
|
||
# Get the current battery status (charging/discharging) | ||
status=$(cat "$battery"/status) | ||
|
||
# get only the name of the battery | ||
# Avoid sending unnecessary notifications | ||
[[ $status = "Charging" ]] && exit 0 | ||
|
||
# Get the name of the battery (BATx) | ||
batteryname=$(echo $battery | grep -Eo "BAT[0-9]") | ||
|
||
# make a beep sound | ||
function makebeep() { | ||
speaker-test -t sine -f 1000 & pid=$!; sleep 0.5s; kill -9 $pid | ||
} | ||
# Check battery status | ||
if [[ $status = "Unknown" || $status = "Discharging" ]]; then | ||
# Make beep sound | ||
makebeep | ||
|
||
# avoid sending unnecessary notifications | ||
[[ $capacity = 100 ]] && exit 0 | ||
# Display notification for $time ms based on charge | ||
if [ $capacity -le 10 ]; then | ||
time=10000 | ||
else | ||
time=6500 | ||
fi | ||
|
||
# if capacity is less than 10% and the battery isn't charging, send a notification (will go away in 30 seconds) | ||
[[ $capacity < 10 && $status = "Discharging" || $capacity < 10 && $status = "Unknown" ]] && makebeep && /usr/bin/notify-send -u critical -t 30000 "Battery $batteryname charge very low " "Please charge battery $batteryname, its capacity is $capacity%." && exit 0 | ||
# if capacity is less than 20% and the battery isn't charging, send a notification (will go away in 30 seconds) | ||
[[ $capacity < 20 && $status = "Discharging" || $capacity < 20 && $status = "Discharging" ]] && makebeep && /usr/bin/notify-send -u critical -t 30000 "Battery $batteryname charge low " "Please charge battery $batteryname, its capacity is $capacity%." | ||
# Send notification | ||
/usr/bin/notify-send -u critical -t $time "[$batteryname] Battery charge low" "Please charge battery $batteryname ($capacity%)." | ||
fi | ||
done |