I have a volume (saved as an encrypted sparsebundle) that I use from time to time. For security reasons it should be ejected when not in use. Of course I should do it myself, and usually I remember. But not always. Is there an automated way to do this as a back-up for fallible me?

Maybe a Folder Action attached to it that will eject it after a certain length of time (or even better, a certain length of time idle) or at a certain time of day?

Maybe a way to have it automatically ejected when the computer sleeps? Currently, it is still there after computer sleep. (I don't want to use logout because there are other processes I want to keep going.)

  • 1
    Do you have a file from within the volume open in an application while you "use" the volume? Maybe just a cronjob/LaunchAgent attempting to unmount the volume every 15' (and failing as long as a file is open) would work then. – nohillside Dec 26 '17 at 13:37

Thanks to patrix, I now have it working. I will post something here for others who want to do this in the future.

It seems that cronjob is no longer recommended, instead launchd. This was the first time I wrote a launch agent, so there were many errors before I got it to work. I could not decipher the error messages in the System log, but at least when they were there it told me it was not working.

I found a tutorial for a launch agent on robots.thoughbot.com. Some of the ways he uses launchctl are marked "legacy" by Apple, but they still worked for me in Sierra.

Here is my launch agent plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

It is supposed to execute the Unix command diskutil unmount force /Volumes/3D every 10800 seconds (= 3 hours).

  • Adding the LaunchAgent plist and the script it calls (if any) would be great, both for future visitors and as an opportunity to get more eyes to review your code (and give feedback if necessary). – nohillside Dec 27 '17 at 18:42
  • Just a thought.....instead of forcing an unmount every three hours, you could run a script every 30 mins that checks if /var/sleepimage was modified and if it was, then unmount the volume. This would mean that it just came out from sleep (which means no access) and you would be limited to a Window of only 30 mins. Conversely, if you happen to be working for 3 hrs and 1 min, it won't eject the volume on you surreptitiously. – Allan Dec 27 '17 at 19:54
  • The hard counter is tricky anyway. If you start using the volume 5 minutes before the agent runs it will eject it even while you are still working with its content – nohillside Dec 27 '17 at 20:27

You may find the following project useful : http://henri.shustak.org/tools/ejector

  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – grg Aug 7 at 22:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .