Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to eject and spin down external drives System
This hint addresses two recurring problems of mine: the obnoxious loudness of my external hard drive, and its refusal to remount itself in a timely and orderly fashion. You'd think it would be enough to simply tell the Finder to Eject the HD -- but that won't do. Frequently, ejecting the drive will remove it from the system, but will NOT spin it down: the damn thing continues to whirr abusively in its casing! How might one silence an abrasive drive?

Read on for the answer, along with a handy re-mount script...

Open your Script Editor (in /Applications/Utilities), and paste the single line of code below:
do shell script "touch /Volumes/Your_Drivename/.dummy; 
  hdiutil eject `disktool -l | grep Your_Drivename | 
  awk '{ print substr($0,20,7) }'`"
As noted above, that should be one line -- replace the line breaks with a single space when entering into Script Editor.

Substitute the name of your external drive (simply as it appears on your Desktop and in the Finder) for Your_Drivename. Hit the Compile button in the Editor's toolbar. Select "Save As..." and be certain to indicate it as an "Application Bundle" in the "File Format" pull-down menu. Tick the "Run Only" checkbox, and name the thing "Eject FW Drive", or "Shutup!," or what have you, and tuck it away in your Utilities folder.

ASIDE: For those of you unfamiliar with the command line, there are two commands in there, separated by a semicolon. The first one, touch, will perform a trivial alteration on an invisible file named .dummy, bumping the "Last Modified" date to the current day and time -- if the file does not exist, touch will create it.

For some inexplicable reason, Mac OS X and its UNIX undercarriage absolutely refuse to spin down an external HD when asked to eject it, unless some file on the drive has been modified. A little touch is all that's needed: followed by the command to eject Your_Drivename (again, substituting in the name as it appears in/on the Desktop/Finder), this will vanish and silence your external drive!

Now, to keep this full-fledged application out of your Dock, simply perform the NSUIElement hack. It will now only run in the background, quite invisibly, never appearing in your Dock ... and thanks to Run Only, will also quit instantly. If you've got a hotkeying program like Butler, Spark, or (Steve help you) QuicKeys, set it up to run this. Bingo: whenever this is run, your external HD will disappear and become completely inaudible -- you'll receive no notification but its blissful silence.

Of course, this still leaves the problem of how to get the thing up and mounted again. I remember reading Hints about having to unplug the drive, then your laptop, then plug the drive and laptop in again, then run System Profiler ... and even then, you might have to logout and login again to get the damn thing to appear!

I said to myself, "Self, there's gotta be a better way..." And here it is ... get the Script Editor front and center once again. Paste this into it:
do shell script "diskutil mountDisk `disktool -l |
  grep Your_Drivename | awk '{ print substr($0,20,7) }'`"
Again, that's one line (replace the return with a space), sub in your drive's real name for Your_Drivename, and hit Compile. Again, save it as "Run Only" and as an "Application Bundle" -- name it "Mount", or maybe "Arise!" Perform the NSUIElement trick, and bind it to a convenient hotkey. Whenever you run this thing, your detached and silent external HD will suddenly spring to life and mount itself. Presto: no more mucking about with cables or unneeded applications. NOTE: credit for this hint should be divided evenly among the members of Ars Technica's Macintoshian Achaia represented in this thread. Special thanks to new Achaian MitchM for his marvellous first-post contribution!

[robg adds: My external drive definitely spins down when unmounted, but the reattach script above is very useful -- no more pulling the power cable to get it to show itself again.]
    •    
  • Currently 3.60 / 5
  You rated: 3 / 5 (5 votes cast)
 
[17,858 views]  

A script to eject and spin down external drives | 15 comments | Create New Account
Click here to return to the 'A script to eject and spin down external drives' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to eject and spin down external drives
Authored by: coogan1971 on Apr 06, '04 12:41:14PM

I tried to get this to work, but all it generates is a error that says it doesn't recognize the name of the drive ("LaCie HD").



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: olivesoft on Apr 06, '04 01:21:07PM

Since you have a space in the name of your external volume, you'll either have to quote the name or escape the space character by preceding it with a backslash character.

---
-Bob
---------------------
I tend to think of [Mac] OS X as Linux with QA and Taste.
-James Gosling, Java Architect



[ Reply to This | # ]
shell command to (re)mount volumes
Authored by: zeorge on Apr 06, '04 01:17:57PM

:) this little script (re)mounts any unmounted volumes !
(auto detected - so you dont have to set any volume name)

1. copy/paste the script (starting with #!/usr/bin/perl) to bbedit
2. save it as "/bin/mountall" (may need admin password)
3. type "sudo chmod +x /bin/mountall" in the terminal
4. type "rehash" in the terminal
now you can use it with "mountall" in the terminal

for us who like to type instead of clicking ;)
zeorge 040406

[code]
#!/usr/bin/perl
# mountall [zeorge 04-2004]
@line_in = `disktool -l | grep volName`;
foreach $line (@line_in) {
$dev = extract($line,"Appeared ('","'");
$mp = extract($line,"Mountpoint = '","'");
$vn = extract($line,"volName = '","'");
if($mp eq "" && $vn ne "") {
print "mounting volume $vn ($dev) ... ";
print `diskutil mount $dev`;
}
}
sub extract {
my $string = shift;
my $start = shift;
my $end = shift;
my $pos = index($string,$start) + length($start);
$string = substr($string,$pos);
$pos = index($string,$end);
return substr($string,0,$pos);
}
[/code]



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: simsamsep on Apr 06, '04 03:40:41PM
My external FW drives always spin down when unmounted. I can quickly and easily mount them again using the freeware "Mount Me!" script. (See: http://www.versiontracker.com/dyn/moreinfo/macosx/15077.

---
simsamsep

[ Reply to This | # ]

perfect...
Authored by: hillbilly on Apr 07, '04 12:46:34AM

This is a great little script! I had been using Disk Utility to mount / unmount my FW hard drive, but this method (like you said) does not keep the drive from spinning, sometimes for days between backups. I had resigned myself to the irritating plug/unplug method until now.. Thanks a lot!



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: googoo on Apr 07, '04 09:09:07AM

I use this command to remount my external FireWire hard drive:

diskutil mount /dev/disk1

I have it aliased to "fwmount". I suspect that it would not work if you have multiple external drives, though.

-Mark

[ Reply to This | # ]

A script to eject and spin down external drives
Authored by: belette on Apr 07, '04 04:44:48PM

Is there a way to make the remount script work with an iPod?



[ Reply to This | # ]
Here's a perl script to mount and unmount by volume name
Authored by: fix on Apr 08, '04 01:18:02AM

This script combines a couple of the great ideas above into one perl script that will mount and unmount a drive based on the that name that it appears as on the desktop (i.e. no /dev stuff). It also uses touch to write a file before unmounting.

Like the others, I use it to mount my firewire drive before doing a backup, and then I unmount it so it'll spin down.
----------
Usage:

Mount a drive:
hd -m driveName

Unmount a drive:
hd -u driveName
-------------
To use this on your machine (just like the other perl example above)

1. Copy the text into a new text file called 'hd'
2. >chmod +x hd
3. Copy the file somewhere into your path
4. >rehash
--------------------------------- Perl script starts at next line:<code>
#!/usr/bin/perl

use warnings;
use strict;
use Getopt::Std;

our $OPTIONS = {};
getopts('mu',$OPTIONS);

our $vol_name = $ARGV[0] || 'zbackup';
our $vol_regex = "volName\\s*=\\s*'$vol_name'";


our @entries = grep(/$vol_regex/o, `disktool -l`);

if (! @entries) {
die "No such volume '$vol_name'";
} elsif ($#entries > 0) {
warn("More than one mountable partition matching $vol_name, mounting the first one.");
}

if ($entries[0] =~ m|\('([a-zA-Z0-9]+)'\s*,\s*Mountpoint\s*=\s*'(.*?)'\s*,\s*fsType\s*=\s*'(\S*?)'|) {
my $disk = $1;
my $mount = $2;
my $fs = $3;
if ($OPTIONS->{u}) {
if ($mount) {
`touch $mount/.dummy`;
`disktool -u $disk`;
}
} elsif (! $mount) { #if you pass no flags it'll also mount
`disktool -m $disk`;
}
} else {
die("Entry did not match expectations: $entries[0]");
}


exit;

__END__
</code>



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: billbarstad on Apr 08, '04 11:32:37AM

This script apparently does not work if the external drive is partitioned.



[ Reply to This | # ]
Need help to eject and mount partitioned firewire drives
Authored by: groundspeed on Apr 25, '04 10:30:20PM

I have an external firewire drive with two partitions. Normally I can eject or mount them from terminal like this:
-disktool -l ( this lists all their device numbers)
-disktool -m disk2s10 disk2s9 (mounts both partitions)

I want to use this shell script except for both devices but can't figure out the syntax to insert the space between the device numbers:

do shell script "disktool -m `disktool -l | grep 'Partition 1' | awk '{ print substr($0,20,7) }'` "

I need to stick this part after a space for the second device:
`disktool -l | grep 'Partition 2' | awk '{ print substr($0,20,8) }'`

Notice that I change the number of characters in the substring to 8 because I know the device number is disk2s10. (8 characters) I know that's poor form and would like to look for the ' in the string to know when I'm at the end of the device name but I'm not that good a programmer. Can someone who is help me out, or at least point me to a good UNIX reference place.
Thanks

---
When I die, I want to go peacefully in my sleep, like my Grandfather, not screaming in terror like his passengers.



[ Reply to This | # ]
A script to eject and spin down external drives: Modified
Authored by: morphis on Dec 10, '04 01:29:08PM
I modified the AppleScript to only grep for volName The result is that it now works for both volumes that have user defined mountpoints and Volumes that have spaces in there names.

Also there is only one thing you have to edit .
And there are now error messages.
###########################################
set VolToSpinDown to "<YourVolumeName>"

if (do shell script "echo `disktool -l | grep -c \"volName = '" & VolToSpinDown & "'\"`") as number is 1 then
	set VolInfo to do shell script ¬
		"echo `disktool -l | grep \"volName = '" & VolToSpinDown & "'\"`"
	
	set VolPath to do shell script ¬
		"echo \"" & VolInfo & "\" |  awk -F \"'\" '{ print $4 }'"
	if VolPath is not "" then
		set VolDev to do shell script ¬
			"echo \"" & VolInfo & "\" |  awk -F \"'\" '{ print $2 }'"
		if VolDev is not "" then
			--########################################	
			try
				do shell script "touch " & VolPath & "/.dummy"
			on error the error_message number the error_number
				DisplayError("[" & (error_number as text) & "]
" & error_message)
			end try
			try
				do shell script "hdiutil eject " & VolDev
			on error the error_message number the error_number
				DisplayError("[" & (error_number as text) & "]
" & error_message)
			end try
			--########################################	
		else
			DisplayError("Could not get Device for Volume " & VolToSpinDown)
		end if
	else
		DisplayError("Volume " & VolToSpinDown & " is not Mounted")
	end if
else
	DisplayError("Volume " & VolToSpinDown & " is not Connected")
end if

on DisplayError(message)
	display dialog message buttons {"Cancel"} default button 1 with icon stop
end DisplayError
###########################################

[ Reply to This | # ]
A script to eject and spin down external drives: Modified
Authored by: Baby Bloc on Feb 07, '05 03:17:29PM

Is there any way to spin down external firewire drives that are partitioned? The scripts I see all require single volume names...



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: Baby Bloc on Feb 07, '05 03:22:31PM

Any way to make this work with partitioned drives? I usually unplug the thing to make it shut-up, but it'd be nicer if it just spun-down.



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: sweek on Feb 11, '05 12:45:47PM

Same problem here...
I just copy pasted the string and that way I can make it un and remount both volumes, but it won't actually spin down.



[ Reply to This | # ]
A script to eject and spin down external drives
Authored by: ssevenup on Mar 16, '07 04:22:24PM

For anyone referring back to this Hint, disktool is deprecated in Tiger.

---
Mark Moorcroft
ELORET Corp. - NASA/Ames RC
Sys. Admin.



[ Reply to This | # ]