Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time


By default MacOSX uses SafeSleep when your MacBook goes to sleep. This means the contents of your ram will be written to disk everytime you close your MB. If you lose battery power, the system can restore the contents of the ram by reading the data from disk. Handy, but always necessary? Don't think so!

This little tool will write the ram contents to disk only if your battery percentage is below a certain threshold, say 15%. If the amount of battery left is above that threshold, your MB will go to sleep quickly without writing anyting to disk. We can assume you connect to power before your battery is fully empty, because when in sleep the MB will use little power.


  • No waiting for your MB to fully enter sleep
  • No excessive writing to your poor SSD everytime you close your MB
  • SafeSleep when you need it, only below a certain threshold
  • Daemon uses almost no resources, it only listens for closing the lid!

This uses a modified version of SleepWatcher by Bernhard Baehr, all credits go to him!



Please make sure the directories are right, you probably don't want to leave it on your Desktop! Check config/de.bernhard-baehr.sleepwatcher.plist to make sure it links to the correct shell script.

$ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8
$ cd ~/Desktop/autosafesleep/
$ sudo cp sleepwatcher /usr/local/sbin
$ sudo cp sleepwatcher.8 /usr/local/share/man/man8
$ sudo cp config/de.bernhard-baehr.sleepwatcher.plist /Library/LaunchDaemons/
$ sudo launchctl load /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist

Below you'll find the original readme from SleepWatcher:

SleepWatcher ReadMe

SleepWatcher is a command line tool (daemon) that monitors sleep, wakeup and idleness of a Mac. It can be used to execute a Unix command when the Mac or the display of the Mac goes to sleep mode or wakes up, after a given time without user interaction or when the user resumes activity after a break or when the power supply of a Mac notebook is attached or detached. It also can send the Mac to sleep mode or retrieve the time since last user activity.

A little bit knowledge of the Unix command line is required to benefit from this software.

The current version removes the installer packages and requires manual installation. This hopefully prevents people without sufficient Unix knowledge to install this software. When you don't feel familiar with the following installation hints, you are probably not the target audience of this software.

Upgrade from Version 2.0.5 (or below)

For the following steps, we assume that you have unpacked the SleepWatcher 2.2 download on your desktop, i.e. you have the folder ~/Desktop/sleepwatcher_2.2 with this ReadMe as ~/Desktop/sleepwatcher_2.2/ReadMe.rtf.

  1. Kill the running daemon and remove the old startup item:

    $ sudo killall sleepwatcher $ sudo rm -rf /Library/StartupItems/SleepWatcher

  2. Copy the new binary and man page:

    $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8

  3. Copy the launchd agent configuration and the updated rc scripts (when you have modified them, compare your version and the new one and update them as required):

    $ sudo cp ~/Desktop/sleepwatcher_2.2/config/de.bernhard-baehr.sleepwatcher-20compatibility.plist /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist $ sudo cp ~/Desktop/sleepwatcher_2.2/config/rc.* /etc

  4. Load the launchd agent configuration, this starts the SleepWatcher daemon:

    $ sudo launchctl load /Library/LaunchDaemons/de.bernhard-baehr.sleepwatcher.plist

Now SleepWatcher runs again in an environment that matches the version 2.0 environment: users of the Mac can create executable files .sleep and .wakeup in their home directories that are executed when the Mac goes to sleep mode or wakes up. When you reboot the Mac, SleepWatcher is started automatically via launchd.

When you use SleepWatcher on a single user Mac and only want to execute your own .sleep and .wakeup scripts, it is suggested not to run SleepWatcher as a system wide daemon with root permissions, but to run your own instance of SleepWatcher. To achieve this, after performing the above steps 1 and 2, continue with step 5:

  1. Remove the old global rc scripts:

    $ sudo rm -f /etc/rc.sleep /etc/rc.wakeup

  2. Copy the launchd agent configuration for a local user instance of SleepWatcher:

    $ mkdir ~/Library/LaunchAgents $ cp ~/Desktop/sleepwatcher_2.2/config/de.bernhard-baehr.sleepwatcher-20compatibility-localuser.plist ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

  3. Load the local launchd agent configuration, this starts the SleepWatcher daemon with your user account:

    $ launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

Now SleepWatcher runs as a user agent that is stopped when you log off from the Mac and that is restarted when you log in again. Note that you must run SleepWatcher as a user agent when you want to use the options -t -i or -b -r (execute a command after a given time of user inactivity or when the user resumes its activity after a break), because these options require the Mac OS X GUI running, and this is not the case when SleepWatcher is started as a system wide daemon while booting.

Upgrade from Version 2.1 and later

For the following steps, we assume that you have unpacked the SleepWatcher 2.2 download on your desktop, i. e. you have the folder ~/Desktop/sleepwatcher_2.2 with this ReadMe as ~/Desktop/sleepwatcher_2.2/ReadMe.rtf.

  1. Replace /usr/local/sbin/sleepwatcher and /usr/local/share/man/man8:

    $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8

  2. Kill the running SleepWatcher processes and let launchd restart them:

    $ sudo killall sleepwatcher

Installation for new SleepWatcher users

When you never have used SleepWatcher before, the following steps are suggests, assuming that you have unpacked the SleepWatcher 2.2 download on your desktop, i. e. you have the folder ~/Desktop/sleepwatcher_2.2 with this ReadMe as ~/Desktop/sleepwatcher_2.2/ReadMe.rtf.

  1. Install the SleepWatcher software:

    $ sudo mkdir -p /usr/local/sbin /usr/local/share/man/man8 $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher /usr/local/sbin $ sudo cp ~/Desktop/sleepwatcher_2.2/sleepwatcher.8 /usr/local/share/man/man8

  2. Read the man page and think about the features of SleepWatcher you want to use:

    $ man sleepwatcher

  3. Write small shell scripts that perform the actions you want to trigger by SleepWacher and test them.

  4. Test your scripts in combination with SleepWatcher, started in the foreground in a Terminal window, e. g.:

    $ /usr/local/sbin/sleepwatcher --verbose --sleep /path/to/your/sleepscript --wakeup /path/to/your/wakeupscript ...

  5. Put your SleepWatcher command line into a launchd agent configuration file. As an example you can use one of the plists from ~/Desktop/sleepwatcher_2.2/config. For more information about launchd and its configuration, see the man pages for launchd and launchd.plist and

  6. Copy your launchd agent configuration to /Library/LaunchDaemons or ~/Library/LaunchAgents, depending on whether you need a system wide daemon or a user agent.

  7. Load the launchd agent configuration using launchctl (see examples above and the launchctl man page).

Version History

  • 24.12.2002 version 1.0 first public release
  • 12.01.2003 version 1.0.1 SleepWatcher is now compatible with Mac OS X 10.1 (SleepWatcher 1.0.1 is compatible with Mac OS X 10.1-10.3)
  • 05.05.2005 version 2.0 SleepWatcher evolved to a more general activity monitor (SleepWatcher 2.0 is compatible with Mac OS X 10.3-10.4)
  • 08.06.2005 version 2.0.1 more robust /etc/rc.sleep and /etc/rc.wakeup scripts, installer packages no longer disturb directory permissions
  • 31.12.2005 version 2.0.2 added -E option to execute a command when the display undims without having slept before, /etc/rc.sleep and /etc/rc.wakup now only execute sleep or wakeup scripts of users currently logged in
  • 07.01.2006 version 2.0.3 Mac OS X 10.3 compatibility was broken in version 2.0.2
  • 28.04.2006 version 2.0.4 SleepWatcher is now a Universal binary, /etc/rc.sleep and /etc/rc.wakeup now handle user names with more than eight characters correctly
  • 02.11.2007 version 2.0.5 SleepWatcher is now compatible with Mac OS X 10.5
  • 02.04.2010 version 2.1 added -b -r, -P and -U options; removed the installer packages and switched to launchd for starting SleepWatcher (SleepWatcher 2.1 runs with Mac OS X 10.5 or 10.6)
  • 06.02.2011 version 2.1.1 fixed a bug in argument parsing (when using only the -P or -U option, SleepWatcher 2.1 complained about "no useful options"); now the idle timer (for the -t -i option) is reset when the Mac wakes up, so the idle command is executed even when the wakeup took place without user interactivity (scheduled wakeup or Wake On LAN)
  • 25.07.2011 version 2.1.2 removes the syslog warning "IOHIDDeviceRegisterInputValueCallback called with a null context" that appears with Mac OS X 10.7
  • 28.08.2011 version 2.2 added the -c and -R options; improved specification of the command line syntax for SleepWatcher

Copyright (C) 2002-2011 Bernhard Baehr (

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see


SafeSleep for your MacBook below a certain battery percentage



No releases published


No packages published
You can’t perform that action at this time.