Hibernate a Mac OS X server without losing internet/windows file sharing

Internet Sharing and SMB stop a mac going to sleep. If you want to find out what’s stopping your mac going to sleep, you can run pmset -g assertions. Furthermore, the mac does not allow hibernation. I’m a bit over-energy-conscious, so here is my solution to having an energy-efficient home server.

Tell power management to hibernate instead of sleep (optional): sudo pmset -a hibernatemode 25

Create a new file, like “gotosleep.sh”, and insert these commands:
launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist
launchctl unload /System/Library/LaunchDaemons/smbd.plist
pmset sleepnow
launchctl load /System/Library/LaunchDaemons/smbd.plist
launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist


launchctl unload /System/Library/LaunchDaemons/org.apache.httpd.plist
launchctl unload /System/Library/LaunchDaemons/com.apple.InternetSharing.plist
launchctl unload /System/Library/LaunchDaemons/com.apple.smbd.plist
pmset sleepnow
sleep 120s
launchctl load /System/Library/LaunchDaemons/com.apple.InternetSharing.plist
launchctl load /System/Library/LaunchDaemons/com.apple.smbd.plist
launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Then make the file executable:
chmod 700 gotosleep.sh

When you want the mac to sleep for the (e.g.) night, just enter:
sudo ./gotosleep.sh
at the command prompt. The mac will sleep a little while after the first three commands (during the pause), and upon manual restarting (power button) will continue with the last three. So sleep is possible due to disabling the services, and they start up again afterwards. If you are running other services, just google around for the appropriate shell command. I’m not too sure about smbd – it seemed to be necessary, but I always get a message that it’s already started.

If you use hibernation, you can even unplug/switch off the power…! I haven’t tried scheduled wake-up yet, but it might be a nice feature to have – manual switch off but always ready for service by breakfast.

UPDATE: We’ve been having some problems that although Internet Sharing appears to work after restarting, it actually has to be unloaded and loaded before working after waking up. For now I took out the “load” items in the script and start SMB and Internet Sharing manually, I’ll update later with a better solution.

2nd UPDATE: This wasn’t the only problem, but overall I think the edited version above is now working well.

References: