Created
February 20, 2014 02:37
-
-
Save adamlazz/9106046 to your computer and use it in GitHub Desktop.
Send pushover notification when Mac battery gets low
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
#!/bin/bash | |
# Usage ./battery-pushover.sh <percent threshold> | |
batterypct() { | |
pct=`ioreg -n AppleSmartBattery | grep -i capacity | tr '\n' ' | ' | awk '{printf("%d", $10/$5 *100 )}'` | |
echo $pct | |
} | |
send() { | |
curl -s \ | |
-F "token=<use your own>" \ | |
-F "user=<use your own>" \ | |
-F "title=Battery low" \ | |
-F "message=Battery below $1%" \ | |
https://api.pushover.net/1/messages.json > /dev/null | |
} | |
if [[ ! -e ~/".bat-lock" ]]; then | |
threshold=$1 | |
pct=$(batterypct) | |
if [[ "$pct" -le "$threshold" ]]; then | |
send "$threshold" | |
touch ~/.bat-lock | |
while : ; do | |
sleep 60 # 1 min | |
pct=$(batterypct) | |
if [[ "$pct" -gt "$threshold" ]]; then | |
rm ~/.bat-lock | |
break | |
fi | |
done | |
fi | |
else | |
echo "~/.bat-lock exists." | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment