# Eject secondary internal hard drive on boot & wake on a Mac, using LaunchAgent scripts

/ 3评 / 0

Eject secondary internal hard drive on boot & wake on a Mac, using LaunchAgent scripts

Recently – after 2 days of research, 3 days of shipping, and several hours of courage building – I went inside my mid-2010 iMac and installed an additional 240GB SSD. I did this because my Seagate Barracuda 1TB @7200 (HDD) was horribly grumbly and sluggish to spin-up. I hate it, I always have done.
So, I’m now sitting with an iMac with two internal drives – one HDD, one SSD. As the geeky, LotR meme would go, “One cannot simply swap an iMac hard drive”. Although not strictly true, it is true in most cases of upgrades…
The iMac hard drive thermal sensor problem
Apple have used proprietary thermal sensors on their iMac range since late 2009. This massively limits the number of drives you can simply switch with your original, and essentially narrows the SSD options to nil. It becomes even more of a headache from 2011 onwards, too.
Should you choose to leave the thermal sensor completely disconnected from any drive you slot into the 3.5″ HDD bay, you will find your iMac blasting air through it’s insides at 4-6000 RPM! You can use software to counter this, but it obviously comes with some risks. There’s a brilliant video and respective blog post by Ryan Trotz that discusses possible solutions for this problem using software.
I wanted the HDD in there to prevent fan trouble and use it as an archive drive, but I don’t want it spinning at all during the time I’m using my Mac unless I specifically allow it to. I found a near perfect solution, and here’s how I did it…
Ejecting an internal hard drive at boot on a Mac
To force an internal hard drive to spin-down during the time you’re using your Mac, you’ll need to eject it completely. You can eject an internal hard drive that is not the boot drive using the following command in Terminal (in/Applications/Utilities/Terminal.app), without the curly brackets:

diskutil eject disk0

Alternatively, if you only know the volume name (or any partition on the drive):

diskutil eject '/Volumes/{Any partition name on the drive}'

I’ll be showing you two solutions which use both above methods. We’ll first need to create a new shell script that we can execute on boot and wake. So open up Terminal again and type the following commands:

mkdir ~/Library/Scripts
touch ~/Library/Scripts/ejecthdd.sh
open -e ~/Library/Scripts/ejecthdd.sh

Once you’ve got the new script file open, paste the following code in there and save it:

diskutil eject disk0
diskutil eject disk1

Don’t worry, it will not eject your boot drive – ie. the drive you’re currently running Mac OS X from.
Alternative for ejection via volume name:

volumeName="Secondary HD Partition name"

#check if mounted, if not then mount both drives
if ! mount | grep $volumeName ; then diskutil mountDisk disk0 diskutil mountDisk disk1 fi #now unmount the drive holding your chosen partition name diskutil eject "/Volumes/$volumeName"

Save whichever script method you went for and close down TextEdit for now. Let’s make your script run on system boot using a LaunchAgent. Create a new .plist file inside/Library/LaunchAgents using the following commands:

sudo touch /Library/LaunchAgents/com.andylockett.ejecthdd.plist
sudo open -e /Library/LaunchAgents/com.andylockett.ejecthdd.plist

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEplist PUBLIC "-//Apple//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plistversion="1.0">
<dict>
<key>Label</key>
<string>com.andylockett.ejecthdd</string>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
</array>
<true/>
<key>KeepAlive</key>
<false/>
</dict>
</plist>

You can now give your Mac a restart to test this. Hopefully just before the desktop shows (or login screen if you use that), you should hear your chosen hard drive spin down.
Now all we need to tackle is the issue of waking up from sleep – the Mac spins the HDD back into action, but does not re-mount it. Awkward.
Eject an internal hard drive after waking a Mac
I used Sleepwatcher to create this solution. Sleepwatcher can execute shell scripts on sleep, on wake, and on various other related events. We want to use the wake up functionality to eject our secondary HDD again. Install Sleepwatcher on your Mac before you read on…
Now that you’ve got Sleepwatcher installed (you ran the README tests, right?), we can create our final LaunchAgent to call our ejecthdd.sh script. This is where those extra lines come in handy in that script. We re-mount the drive before ejecting after wake up since we cannot eject the already unmounted drive (unmounted at boot).

sudo touch /Library/LaunchAgents/com.andylockett.ejecthddonwake.plist
sudo open -e /Library/LaunchAgents/com.andylockett.ejecthddonwake.plist

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEplist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plistversion="1.0">
<dict>
<key>Label</key>
<string>com.andylockett.ejecthddonwake</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/sbin/sleepwatcher</string>
<string>-V</string>
</array>
<true/>
<key>KeepAlive</key>
<true/>
</dict>
</plist>

That should be it! Again, give your Mac a restart and you should hear the spin-down upon boot. Wait a few minutes then put your Mac to sleep and wake it up. You should hear the hard drive spin into action, only to be spun-down a few seconds later via our script and Sleepwatcher together.
If anyone has any better solutions, please do leave a comment – I wrote this article because it took me so, so long to put together a solution for this. I’m not the most experienced UNIX guy, I only hope this will help get a few people on the right path to a solution appropriate for them.

 sudo pmset -a disksleep 1

### 3条回应：“Eject secondary internal hard drive on boot & wake on a Mac, using LaunchAgent scripts”

1. Nichol说道：

Remarkable issues here. I’m very satisfied to look your post. Thanks a lot and I am having a look forward to touch you.

2. kicau说道：

Wow! Finally I got a web site from where I be capable of truly take useful
data concerning my study and knowledge.

3. calvinturpin说道：

Great post.

• 默认
• 护眼
• 夜晚
• Serif
• Sans