One of the features I want with my laptop is to be able to put it to sleep and be ready to go. Ideally I can accomplish this by simply closing the lid.
There are two issues that prevent me from doing this currently on OS X Lion:
If I’m using an external monitor, instead of going to sleep, Lion moves all the windows to the external monitor1.
When I’m at home, I always have external drives connected to my laptop. I have to consciously unmount the drives or else I get the dreaded “disk not ejected properly” message when I wake up the laptop.
Currently there doesn’t appear to be a way to affect the lid closing behavior so I just have to manually put the laptop to sleep. However, there is a way to automatically unmount external drives on sleep.2
The primary tool I’m using is SleepWatcher, a daemon which monitors sleep and wakeup events of a Mac. I’ll walk through the installation of SleepWatcher and the creation of associated scripts to unmount external drives on sleep and mount them on wakeup.
Installing SleepWatcher
If you use MacPorts, it’s as simple as:
sudo port install sleepwatcherto install the binary, man page, launchd configuration file, and system-wide sleep and wake up scripts.sudo port load sleepwatcherto load the launchd configuration.
If you don’t use MacPorts, download the latest version of SleepWatcher from http://www.bernhard-baehr.de/ and follow the instructions in the ReadMe.rtf.
Create the .sleep script
The system-wide sleep script iterates through each user’s home directory looking for a .sleep script and executes the script as the user3. Follow these steps to create the script:
cat > $HOME/.sleep- Paste the following into your terminal:
#!/bin/sh osascript -e 'tell application "Finder" to eject disks'
- ⌃D (Ctrl-D) to terminate the input stream.
chmod +x $HOME/.sleep
At this point you can test to see if your script is working properly by running $HOME/.sleep. Your external hard drives should be unmounted.
Create the .wakeup script
This script is used to mount any HFS volumes that the laptop knows about upon waking up. Follow these steps to create the script:
cat > $HOME/.wakeup- Paste the following into your terminal:
#!/bin/sh
/usr/sbin/diskutil list | \
awk '/Apple_HFS/ {print $NF}' | \
xargs -I{} /usr/sbin/diskutil mount {}
- ⌃D (Ctrl-D) to terminate the input stream.
chmod +x $HOME/.wakeup
Test the script by running $HOME/.wakeup. Your external drives should be mounted.
That’s it. Now when you sleep your computer, any external drives should automatically be unmounted4. Upon wakeup, any HFS volumes should automatically be mounted.
-
Basically, with more than one monitor hooked up to the laptop, the lid acts as an on/off switch for the laptop monitor. ↩
-
I’m only dealing with external USB/Firewire HFS drives. The one test I ran with a USB key fob and Samba network share worked fine. Test everything out in your own environment! ↩
-
Since I’m always logged in as an admin user, I created my
.sleepand.wakeupscripts in my home directory. If you log in as a user that can’t mount/unmount drives, then you should modify the system-widerc.sleepandrc.wakeupscripts, or create your.sleepand.wakeupscripts in the admin user’s home directory. ↩ -
There is still a chance that this may not work if a process has a file lock on a specific volume. In my testing this has worked fine with Time Machine, which will stop the backup properly. ↩
-
objectivecrizzo liked this
-
objectivecrizzo reblogged this from mintyfresh
-
mintyfresh posted this