Skip to content

Instantly share code, notes, and snippets.

@futurist
Created November 16, 2016 08:48
Show Gist options
  • Save futurist/699b613cb15662199ceedd96e35e288f to your computer and use it in GitHub Desktop.
Save futurist/699b613cb15662199ceedd96e35e288f to your computer and use it in GitHub Desktop.
Sleep when battery below 20% in MacBook
#!/bin/sh
MODE=`/usr/bin/pmset -g | awk '/hibernatemode/ { print $2 }'`
LEFT=`/usr/bin/pmset -g batt | grep Internal | awk '{ print $2 }' | awk -F % '{ print $1 }'`
if [ $LEFT -lt 30 ] && [ $MODE != 3 ] ; then
{
/usr/bin/logger -t "hibernatemode" "Battery level less than 30%; setting hibernatemode to 3"
/usr/bin/pmset -a hibernatemode 3
}
elif [ $LEFT -gt 50 ] && [ $MODE != 0 ]; then
{
/usr/bin/logger -t "hibernatemode" "Battery level greater than 50%; setting hibernatemode to 0"
/usr/bin/pmset -a hibernatemode 0
rm /var/vm/sleepimage
}
fi
if [ $LEFT -lt 28 ] && [[ $(/usr/bin/pmset -g batt | grep 'Battery Power') ]] ; then
{
pmset sleepnow
}
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment