Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,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 2.86 / 5
  You rated: 1 / 5 (14 votes cast)
 
[66,609 views]  

Automatically eject/mount disks on sleep/wake | 15 comments | Create New Account
Click here to return to the 'Automatically eject/mount disks on sleep/wake' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Concerning FireWire Boot Drives
Authored by: MacTipper on Apr 04, '08 08:09:43AM
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 Apr 04, '08 10:30:13AM

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 Apr 04, '08 10:48:37AM
Automatically eject/mount disks on sleep/wake
Authored by: Steve28 on Apr 04, '08 11:47:38AM

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 Apr 04, '08 10:35:36PM

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 Apr 04, '08 01:58:09PM
... 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 Apr 05, '08 07:15:22AM

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 Apr 05, '08 08:00:00AM
Ooops! I meant "... .wakeup script...".

[ Reply to This | # ]
Automatically eject/mount disks on sleep/wake
Authored by: djc0 on Apr 05, '08 06:52:46PM

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 Apr 08, '08 10:28:20AM

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 Apr 09, '08 12:30:27AM
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 Jun 13, '08 03:17:47AM

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 Dec 16, '09 03:16:05PM

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 Feb 01, '10 01:27:37PM

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 | # ]
MAC Automatically Mount Net Drives on WAKE or START UP
Authored by: kswison on Jan 25, '12 10:18:04AM

For Beginners like ME, Using SLEEPWATCHER and creating the '.wakeup' file

I am new to MAC but have final figured this out how to set up SLEEPWATCHER using this site. Thanks to the people here and lots of reading. Got the SLEEPWATCHER loaded. Installing SLEEPWATCHER is not that user friendly to new MAC users, but take your time and go through it, the instructions are pretty good.

As for creating the '.wakeup' file, I created 'wakeup.txt' in one directory and copied it to ~/
using the following lines of code and copying the code into TERMINAL

Create 'sleep.txt' and 'wakeup.txt' anywhere on your computer. The simplest might be in the head directory ~/ but it really doesn't matter.

Here is the code I used for my 'wakeup.txt' file. I use it to remount my external net drive.

__start code below line_______________________________

#!/bin/sh
exec <"$0" || exit; read v; read v; exec /usr/bin/osascript - "$@"; exit
delay 10
set testnet to 1
repeat with idx from 1 to 5
try
set ping_result to (do shell script "ping -c 1 -t 1 192.168.20.70")
set testnet to 10
on error
if testnet = 1 then
say "Searching for W Net Drive"
end if
beep 3
set testnet to (testnet + 1)
delay 10
end try
if testnet = 10 then
exit repeat
end if
end repeat
if testnet = 10 then
delay 1
mount volume "smb://W-NetDrive/Volume_1"
say "Net Drive Volumes Mounted"
end if
if testnet = 6 then
say "The W Net Drive could not be PINGED on the network"
say " I am unable to mount the W Drive Volumes"
end if
___________________________
end code above line

As a minimum, you need to make these two changes to the above code or it will not work for you.

1) You will need to change the IP address (192.169.20.70) on line 7 (as shown below)
set ping_result to (do shell script "ping -c 1 -t 1 192.168.20.70")
to match the IP address on your Net Drive or the script cannot PING the Net Drive.

2) You will also need to edit the 7th last line (as shown below)
mount volume "smb://W-NetDrive/Volume_1"
to reflect you own net drive name, or the net drive will not mount.

Further to that, you can easily remove the computer's talking if you want. Simply delete all the lines that start with 'say'. However, this only occurs at 'wake up' so it is nice to know what is going on, but edit it as you see fit.

The way this works, is that the first two lines of code (as follows)
#!/bin/sh
exec <"$0" || exit; read v; read v; exec /usr/bin/osascript - "$@"; exit
should not be revised as they set up the rest of the script to be able to process APPLESCRIPT.
(need more infer... see http://hints.macworld.com/article.php?story=20060425140531375)

Hooray!! I don't need to learn shebang and all that other complicated programming stuff.

After the first two lines, you can simply program away in APPLESCRIPT. Use your APPLESCRIPT editor to ensure the code is correct, the copy/paste your APPLESCRIPT into your 'wakeup.txt' or 'sleep.txt' file, after the first two lines of code (as noted above).

Then you need to rename the '.txt. files as follows
wakeup.txt to .wakeup
sleep.txt to .sleep
Place the new '.' files in the head directory ~/
Then you need to change the '.wakeup' and '.sleep' file permissions to executable.

I did that this way....
__________________________________________

Enter the following text into TERMINAL to rename files and create the hidden '.' files.
I just copied in one line of code at a time.

FOR .SLEEP
cd ~/
cp ~/documents/data-k/W_NetDrive_PING/sleep_and_wake_files/sleep.txt ~/.sleep
chmod a+x .sleep

FOR .WAKEUP
cd ~/
cp ~/documents/data-k/W_NetDrive_PING/sleep_and_wake_files/wakeup.txt ~/.wakeup
chmod a+x .wakeup

Basically its

cp path/to/file.txt ~/.wakeup

This copies the file to the ~/ directory and changes the name in one command.

Then change the permissions using

chmod a+x .wakeup

________________________________________________
This works great !

so....Place your computer into SLEEP, then WAKE it up. Your NetDrive should mount automatically and inform you that it should now be mounted. If it cannot be found in order to mount it, it will also tell you that. Change it to what ever you want it to say.

Oh, at this point in time I actually only use '.wakeup', I have not set up anything in '.sleep'
________________________________________________

As for mounting the drives on START UP I use a similar APPLESCRIPT as follows. Using my APPLESCRIPT Editor I created the following script.

____Script starts below the line___________________

set testnet to 1
repeat with idx from 1 to 5

try
set ping_result to (do shell script "ping -c 1 -t 1 192.168.20.70")
set testnet to 10
on error
beep 3
set testnet to (testnet + 1)
delay 10
end try
if testnet = 10 then
exit repeat
end if



end repeat
if testnet = 10 then
delay 1
mount volume "smb://W-NetDrive/Volume_1"
say "Net Drive Volumes Mounted"
end if
if testnet = 6 then
say "The W Net Drive could not be PINGED on the network"
say " I am unable to mount the W Drive Volumes"
end if
____________________________________
script ends above the line

In the APPLESCRIPT Editor I save the file as 'W-PING.scpt', then re-save the file only this time changing the FILE FORMAT to APPLICATION and as 'W-PING.app'

This file 'W-PING.app' is now executable, (if not change it as above, but I think it become executable as soon as it is saved as an '.app' file extension.)

Then go into the APPLE SYSTEM PREFERENCES

Go to category SYSTEM and find 'USERS & GROUPS' and open it.

On the left hand side you will see the 'Current User', and click on it.

On the right hand side at the top look for 'Login Items' and click on it.

You will see some apps that automatically open when logging in.

Below the box you will see a '+|-' box, go click on the '+'

direct the window to the 'W-PING.app' file or what ever you decided to call it and then click 'ADD"

Repeat this for which ever Users need the Net Drive automatically loaded. You will have to be logged in as them in order to be able to change their start up programs at login.

!!!!!!!!!!!!!!!!!!

Hooray, you are done! You computer will now remount the NetDrive Volumes at both START UP and WAKE.

At least it has all worked for me.

I know there are likely other ways of doing this, but this worked for me so I wanted to share it to save other beginners a bunch of hours trying to figure this out. Just as a though, you may also have to copy the W-PING.app file to a location in their User profile where they can find it and set it up as an application to open at START UP.

Good Luck!



[ Reply to This | # ]