Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

Automatically eject/mount disks on sleep/wake System
Using a laptop with permanent external drives can be a bit annoying when you leave the desk -- you have to manually eject all your devices, otherwise you get that dreaded 'Device Removal' dialog. With a little bit of Terminal magic, though, you can automatically eject disks when you sleep the laptop, meaning you can just put the lid down and go. Disks are also reconnected automatically on wake, for when you're just sleeping the computer without going places.

It's all thanks to a little utility by Bernhard Baehr called SleepWatcher, which runs in the background and is triggered by sleep and wake events, calling scripts to perform required actions. Download and install SleepWatcher and its StartupItem. Next, you're going to create ~/.sleep and ~/.wakeup files which SleepWatcher will call upon.

Pull up your favourite text editor and paste the following in: Save the file (name it sleep.txt and save it to the Desktop); make sure it's plain text! Then put the following in another file, called wakeup.txt, also saved to the Desktop: Next, launch Terminal (in Applications » Utilities) and type in the following:
$ mv Desktop/sleep.txt .sleep
$ chmod a+x .sleep
$ mv Desktop/wakeup.txt .wakeup
$ chmod a+x .wakeup
Now, whenever you sleep your machine, the external drives will be ejected automatically. When the machine wakes again, all connected external drives will be reconnected.

[robg adds: I haven't tested this one...]
    •    
  • Currently 5.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[24,388 views]  

Automatically eject/mount disks on sleep/wake | 14 comments | Create New Account
Click here to return to the 'Automatically eject/mount disks on sleep/wake' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
Concerning FireWire Boot Drives
Authored by: MacTipper on Fri, Apr 4 2008 at 8:09AM PDT
After having to go around a year booting off an External HD, I've discovered that you don't need to do this with a FireWire HD. In my experience, you can even charge while it's unplugged. As long as you plug it back in before you wake from sleep there's no problem.

For those interested, I was using an iBook G4 1 Ghz. I don't know if that makes a difference though. Also, it doesn't work if you have a USB device plugged in.

MacTipper

My Mac-Tipping Blog

[ Reply to This | # ]

Automatically eject/mount disks on sleep/wake
Authored by: robdew on Fri, Apr 4 2008 at 10:30AM PDT
I haven't had this happen for wake/sleep in ages. Is this a 10.4 issue? Doesn't happen with external drives on my 10.5.2 macbook.

[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: vocaro on Fri, Apr 4 2008 at 10:48AM PDT
Automatically eject/mount disks on sleep/wake
Authored by: Steve28 on Fri, Apr 4 2008 at 11:47AM PDT
I have been using SleepWatcher, but I get very sporradic results. I have the following .sleep script in my home dir:

#!/bin/sh
/usr/sbin/diskutil eject /Volumes/Backup

The console shows that the .sleep is being executed, but the drive is not ejected and I get the "Device Removal" error. If I run it manually, it works fine. I know the script actually runs.

I *think* I remember this working prior to 10.5.2... but I'm not sure. Any ideas?


[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: johnsawyercjs on Fri, Apr 4 2008 at 10:35PM PDT
I'm not seeing this working under OS 10.5.2 either--it's not ejecting my USB flash drive, plugged into a USB hub--I still get the "Device Removal" error when I wake the Mac from sleep.

[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: CaptDeuce on Fri, Apr 4 2008 at 1:58PM PDT
... you have to manually eject all your devices, otherwise you get that dreaded 'Device Removal' dialog. ... When the machine wakes again, all connected external drives will be reconnected.

Nice work around. But... even though you're using a laptop, should you even be having that problem?

This was happening to me with the new external USB drive which I specifically purchased specifically to use for Time Machine on my new 24 inch iMac. Not only did I have to dismount the drive, I had to turn it OFF. Defeats the whole concept of Time Machine working transparently. I returned the defective drive to be replaced. Problem solved.

I had a similar problem when my iPod mini interfering with waking my PowerMac G4 from sleep. That problem also went away though I can't recall whether it was a bad add-on extension or bad chaining of USB hubs.

---
"Where's my other sock?" - A. Einstein

[ Reply to This | # ]

Automatically eject/mount disks on sleep/wake
Authored by: NicleT on Sat, Apr 5 2008 at 7:15AM PDT

Hi, now how can I remove all this?

I don't know if it's because I'm running 10.5.2 but this mod works erratically. When I close my laptop's lid, it can take a couple of minutes before it fall asleep and sometimes the (Firewire) HD gets unmounted and sometimes not. Why? So, if I want to remove all those files I must find them:

Where are these files :
The .sleep and .awake script files are on the ~/ folder.
I can use Terminal's rm command to delete these script files

Now, for the Bernhard Baehr's SleepWatcher utility, two installers where used:
The SleepWatcher StartupItem seems to be installed in Library/StartupItem/
But where is the sleepwatcher file ?
Is it in /usr/local/sbin/ ?
Is there other files in other places?

Thanks for your help.



[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: NicleT on Sat, Apr 5 2008 at 8:00AM PDT
Ooops! I meant "... .wakeup script...".

[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: djc0 on Sat, Apr 5 2008 at 6:52PM PDT
From the installer:

SleepWatcher Installer:

This package installs the following files:
/usr/local/sbin/sleepwatcher - the sleepwatcher command
/usr/local/share/man/man8/sleepwatcher.8 - manual page for sleepwatcher

SleepWatcher StartupItem Installer

This package installs the following files:
/Library/StartupItems/SleepWatcher - startup script for the sleepwatcher daemon
/etc/rc.sleep - system wide script executed by the sleepwatcher daemon when the Mac goes to sleep mode
/etc/rc.wakeup - system wide script executed by the sleepwatcher daemon when the Mac wakes up from sleep mode

Delete these files and you should be good to go.


[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: V.K. on Tue, Apr 8 2008 at 10:28AM PDT
deeply deeply confused by this hint. I've used my powerbook with several externals for years (from 10.3 to 10.5) and I never had to eject the external drives before closing the lid.

[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: NicleT on Wed, Apr 9 2008 at 12:30AM PDT
Hey, thanks djc0.
Besides, Bernhard Baehr, the SleepWatcher's developer, replied me on this too. Here's his recommendations:
... removing SleepWatcher is very simple:

Drag /Library/StartupItems/SleepWatcher to the trash. This removes the startup item, and with the next restart, SleepWatcher is deactive. To remove the Unix components, you have to execute the following command in a Terminal window:

sudo rm -f /usr/local/sbin/sleepwatcher /usr/local/share/man/man8/sleepwatcher.8 /etc/rc.wakeup /etc/rc.sleep

But it doesn't matter to let these files installed, they do nothing than occupying less then 20 kB of disk space.

So, it was very simple and worked to solve the long sleep delay of my PowerBook and the errors monitored in Console. Now it's clean... unfortunately because I would have liked the mod to work (I thought I could unplug a FW HD when the PB's in sleep with this tweak).

[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: egg1000 on Fri, Jun 13 2008 at 3:17AM PDT
Slight problems with this tip.....

1) It doesn't work, the device eject warning still occurs.
2) It increases the amount of time your mac takes to sleep by alot!
3) It creates a whirly sound on wake, hoorah!

Otherwise useless, now, can someone please advise on how to reverse the changes? I would be very grateful.

MBP2.4

Thanks



[ Reply to This | # ]
I finally got Sleepwatcher to work
Authored by: tomhayes on Wed, Dec 16 2009 at 3:16PM PST
I recently purchased a SmartStrip power outlet for my computer setup to turn off devices when the computer went to sleep.

It worked - except for my Hitachi Simpletech 2GB drive. The external drive has a "power on auto" feature where you plug it into the wall and then into to the computer and only when both the wall outlet and the computer are on does the drive come on.

When the computer went to sleep the Powerstrip would turn off power to the hard drive and the USB port on the Mac would sense that the drive was removed and it would trigger some sort of event where the computer would immediately wake from sleep and I'd get a Device Removal Warning.

I then plugged the external drive into one of the "Always Powered" outlets, but it had the same issue.

I downloaded SleepWatcher (http://www.bernhard-baehr.de/) and installed SleepWatcher and the Sleepwatcher start-up items program.

I created a file in my home directory as named .sleep and chmod r+x it. (note this thread kept talking about Desktop/.sleep , it should be in your home directory which is commonly refereed to as "~" or something like /bootdrive/usr/yourusername)

.Sleep contained the following command:
osascript -e 'tell application "Finder" to eject "2TB"'

Where 2TB was the name of my external drive.

When the computer went to sleep Sleepwatcher started and finder started to eject the drive and the computer went to sleep. The drive did not completely unmount before the computer went to sleep and it triggered some event that would wake up the Mac. The external drive would turn on and mount again with the Device Removal Warning effectively stopping sleep.

So I modified .sleep to contain:
osascript -e 'tell application "Finder" to eject "2TB"'
osascript -e 'tell application "Finder" to delay 10'

Now the drive unmounts, finder waits 10 seconds and then the computer sleeps. It works like a charm.

I created a file called .wakeup that contains:
#!/bin/bash
backupsVolume=`diskutil list | awk '/ 2TB / {print $6}'`
if [ ! -z "$backupsVolume" ]
then
diskutil mount $backupsVolume
fi

This mounts the drive when the computer wakes from sleep.

Thank you to Sleepwatcher, this thread, and Finity.org (http://www.finity.org/node/41) for helping me with this.

P.S. I am running this on a Mac pro with Snow Leopard.


[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: SandraStrength on Mon, Feb 1 2010 at 1:27PM PST
So apparently the SleepWatcher appears to be a not so "easy" fix for the unmounting before sleep problem.

One work around is to use the automator to create an icon you can place on the desktop to put the system to
sleep. This automator sequence can be configured to be run automatically with systems that have auto-sleep as well.

Here is the sequence:
- Step 1: Automator uses Finder to identify the drive(s) you wish to unmount prior to sleep
- Step 2: Automator uses the "Eject Disk" command to eject all the drives you specified
- Step 3: Run the following applescript:

tell application "System Events"
sleep
end tell

Save this as an appropriate application, workflow, or plug-in and use accordingly.

Works consistently, reliably, and uses the under appreciated Automator app. :)

Hope this helps!


[ Reply to This | # ]