On my mid-2009 Macbook Pro, I noticed that after about 1,200 battery cycles the system started to shut down without warning when the battery level was still about 10%. I believe this occurs because the battery cannot provide the amount of power required to keep the system running even though it is not yet completely empty. Off course, it is very inconvenient when the system suddenly turns off, as any unsaved work gets lost (it even thinks it’s 01-01-2001 again). Even though spending 129 euro on a replacement battery would solve this issue, that seemed a bit odd as I could still work on it for 4–5 hours.
Therefore, I wrote a simple shell script that keeps track of the remaining battery time. When it gets below a certain threshold, it changes hibernation mode to the mode whereby the contents of the RAM are stored on the hard disk and after that it enters standby. When there is sufficient power it changes hibernation mode back to the default (the one that is normally entered when the lid is closed).
I have been using it for almost a year now (I am now at 1,450 cycles), and it works precicely as expected. As it might be useful to others as well, I decided to post it here.
I run this script every minute. This can be configured by adding the following to /etc/crontab
Obviously, /Users/stefan/sleep.sh is the location where the script is located on my system.
Therefore, I wrote a simple shell script that keeps track of the remaining battery time. When it gets below a certain threshold, it changes hibernation mode to the mode whereby the contents of the RAM are stored on the hard disk and after that it enters standby. When there is sufficient power it changes hibernation mode back to the default (the one that is normally entered when the lid is closed).
I have been using it for almost a year now (I am now at 1,450 cycles), and it works precicely as expected. As it might be useful to others as well, I decided to post it here.
Code:
#!/bin/sh
MINLEVEL=25
current_mode=`pmset -g live | grep hibernatemode | cut -s -f2`
discharging=`pmset -g ps | grep discharging | cut -s -f1`
power=`pmset -g ps | grep InternalBattery | cut -d' ' -f4`
if [ -z $discharging ];
then
if [ $current_mode -eq 1 ];
then
pmset hibernatemode 3
fi
exit
fi
hr=`echo $power | cut -d':' -f1`
min=`echo $power | cut -d':' -f2`
if [ $hr -eq 0 -a $min -le $MINLEVEL ];
then
pmset hibernatemode 1
pmset sleepnow
exit
fi
if [ $current_mode -eq 1 ];
then
pmset hibernatemode 3
exit
fi
I run this script every minute. This can be configured by adding the following to /etc/crontab
Code:
# m h dom mon dow user command
* * * * * root sh /Users/stefan/sleep.sh
Obviously, /Users/stefan/sleep.sh is the location where the script is located on my system.