Skip to content

Instantly share code, notes, and snippets.

@sach1t
Last active July 21, 2022 02:59
Show Gist options
  • Save sach1t/a32f62bbd2493c23459970c240503f00 to your computer and use it in GitHub Desktop.
Save sach1t/a32f62bbd2493c23459970c240503f00 to your computer and use it in GitHub Desktop.
Sway battery low notifier
#!/bin/bash
bat_low_threshold=$1
poll_interval=60 # seconds
function bat_high {
local cap=$(cat /sys/class/power_supply/BAT0/capacity)
if [[ $cap -lt $bat_low_threshold ]]; then
return 1
else
return 0
fi
}
function nap {
sleep $poll_interval
}
for pid in $(pidof -x "batman.sh"); do
if [ $pid != $$ ]; then
echo "Batman #$pid is already on patrol!"
exit 1
fi
done
while true; do
while bat_high; do nap; done
swaynag --message "Battery is low"
until bat_high; do nap; done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment