Skip to content

Instantly share code, notes, and snippets.

@JoshuaJChan
Created June 2, 2020 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshuaJChan/9e8190304f6be659fe1af6ef6f63cb93 to your computer and use it in GitHub Desktop.
Save JoshuaJChan/9e8190304f6be659fe1af6ef6f63cb93 to your computer and use it in GitHub Desktop.
script to notify low battery using crontab
#!/bin/sh
#requirements: zenity, mpv, acpi, https://www.freesoundslibrary.com/positive-tone-sound-effect/,
count=0 #timer after critical battery range is reached
export DISPLAY=:0 #capture the right monitor to display the popups
#read in status and number charge
read -r status capacity <<<$(acpi -b | awk -F'[,:%]' '{print $2, $3}')
#check if on battery and between 0-4%
if [[ $status == "Discharging" && "$capacity" -le 4 ]]; then
logger "Critical battery threshold"
zenity --warning --height=100 --width=200 --title "Battery Critically Low" --text "Laptop will hibernate in 90 seconds. Please plug in a charger." &
while [[ $status == "Discharging" && "$count" -le 90 ]]
do
sleep 5
let "count+=5"
if [[ "$count" -eq 90 ]]; then
systemctl suspend
fi
read -r status capacity <<<$(acpi -b | awk -F'[,:%]' '{print $2, $3}') #reread the status
done
#check if low battery eg below 15%
elif [[ $status == "Discharging" && "$capacity" -le 15 ]]; then
logger "Low Battery"
mpv Music/SoundEffects/positive-tone-sound-effect.mp3 &
zenity --warning --height=100 --width=200 --title "Battery Low" --text "Battery is low. Please find a power source (Remaining = $capacity)" &
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment