How to unmount a drive with a script

bashscriptsleep-wake

I installed sleepwatcher to run ~/.sleep before my computer goes to sleep.

I need to unmount /Volumes/Backup before my computer falls asleep. If I forget to do it, I get a message telling me that I unsafely unmounted the drive, etc. when the computer resumes. It's a rare external Seagate drive glitch. Lucky me.

So I am trying to remedy the problem with Sleepwatcher by unmounting the drive before the system sleeps. Obviously sudo and the password it requires is standing in my way.

Any ideas?

I can't use the echo "password" | sudo -S xx xx method because my password contains a dollar ($) sign, So I need another form of injection.

If I remove the password requirement from /etc/sudoers, the system becomes very insecure.

So any creative ideas?

Best Answer

You don't need sudo to unmount a drive.

Use diskutil

The following command ejects the volume Backup:

diskutil eject /Volumes/Backup

This should do it, although (IIRC) you might want…

diskutil unmount /Volumes/Backup

…to make it easier to re-mount without having to physically disconnect/reconnect the drive.

Ejecting All Disks

If you want to make sure you have ejected all of your disks, you can try this:

osascript -e 'tell application "Finder" to eject (every disk whose ejectable is true)'

Third Party Tool: Mountain

There is also Mountain which will automate this for you.

Third Party Tool: Keyboard Maestro

Above all other ideas, I would recommend Keyboard Maestro which can do all of these things on sleep, and remount your drive on wake, plus 1,000 other things.