Skip to content

Instantly share code, notes, and snippets.

@adamlazz
Created February 20, 2014 02:37
Show Gist options
  • Save adamlazz/9106046 to your computer and use it in GitHub Desktop.
Save adamlazz/9106046 to your computer and use it in GitHub Desktop.
Send pushover notification when Mac battery gets low
#!/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