Skip to content

Instantly share code, notes, and snippets.

@raphapr
Created February 10, 2017 19:46
Show Gist options
  • Save raphapr/696c52ee5887b100886d23828fe0aa2f to your computer and use it in GitHub Desktop.
Save raphapr/696c52ee5887b100886d23828fe0aa2f to your computer and use it in GitHub Desktop.
#! /bin/bash
########################################
# Check if script already running
lf=/tmp/battery_warning.pidlock
# create empty lock file if none exists
touch $lf
read lastPID < $lf
# if lastPID is not null and a process with that pid exists , exit
#[ ! -z "$lastPID" -a -d /proc/$lastPID ] && exit
[ ! -z "$lastPID" -a -d /proc/$lastPID ] && kill $lastPID
# save my pid in the lock file
echo $$ > $lf
########################################
SLEEP_TIME=5 # Default time between checks.
SAFE_PERCENT=30 # Still safe at this level.
DANGER_PERCENT=20 # Warn when battery at this level.
CRITICAL_PERCENT=10 # Hibernate when battery at this level.
export DISPLAY=:0.0
function notify
{
killall notify-osd
notify-send "Bateria baixa" "Conecte o notebook a um adaptador!" -i /usr/share/icons/Numix/scalable/status/battery-empty-symbolic.svg
}
function notify_n_hibernate
{
killall notify-osd
notify-send "Alerta! " "O computador irá hibernar dentro de 60 segundos" -i /usr/share/icons/Numix/scalable/status/battery-empty-symbolic.svg
sleep 60
if [[ -n $(acpi -b | tail -n 1 | grep -i discharging) ]]; then # Se o notebook continuar fora da tomada, hiberna.
i3exit hibernate
fi
}
while [ true ]; do
if [[ -n $(acpi -b | tail -n 1 | grep -i discharging) ]]; then
#pm-powersave true
rem_bat=$(acpi -b | tail -n 1 | grep -Eo "[0-9]+%" | grep -Eo "[0-9]+")
if [[ $rem_bat -gt $SAFE_PERCENT ]]; then
SLEEP_TIME=10
else
SLEEP_TIME=5
if [[ $rem_bat -le $DANGER_PERCENT ]]; then
SLEEP_TIME=2
notify
fi
if [[ $rem_bat -le $CRITICAL_PERCENT ]]; then
notify_n_hibernate
SLEEP_TIME=1
fi
fi
else
SLEEP_TIME=10
fi
sleep ${SLEEP_TIME}m
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment