Eject Disks and Shutdown... Need some help!

This is possibly a bit niche, and is rather optimistically aimed at any users who may have tried something similar in the past.

Objective

When I leave my studio, I need to eject all external drives so I can disconnect my Macbook Pro and take it away with me. There are two things that mean this isn't totally straight-forward:

  • Time Machine may be in the middle of a backup.
  • If Logic Pro is still running, I'll get an alert saying that my external audio drive is still in use, even though it's not.

My attempt

I've tried to create a macro that does the following:

  • Checks to see if TM is backing up. If it is, I'll be notified and can choose to:

    • Wait and be notified when the backup is complete.
    • Stop the backup now and then continue with shutdown.
    • Cancel the macro.
  • Assuming I didn't cancel, all external drives are ejected via AppleScript.

  • My particular shutdown sequence commences, which involves checking my mouse battery level, muting my speakers, turning off some smart plugs and then putting my mac to sleep.

Here's my existing macro:

Studio Shutdown.kmmacros (65 KB)

Macro screenshot

Issues

If Time Machine is not currently backing up, and Logic Pro has been quit, all disks are ejected and the shutdown is successful. However...

  • I need to do further testing on the section that waits for or stops the current backup, but I don't think it's reliable. Obviously I don't want to repeatedly eject my Time Machine drive while it's in the middle of a backup, for the sake of testing, but I'm fairly sure it has issues and skips on to the ejection stage before due time. I was hoping someone might have ventured down this rabbit-hole before and can suggest a more robust solution.

  • As mentioned at the start of the post, I'll be alerted that Logic Pro is currently using my Audio drive, when AppleScript tries to eject it. The shutdown will then continue and, when I next open my mac, I'll see that this drive was removed before being ejected properly.

By the time I'm ready to shut down, there will be no Logic Pro project open, so the Audio drive is not in use. I wonder if there's some way to force eject the drive without having to quit Logic Pro? I'd prefer not to quit Logic, as it takes several minutes to start up again these days (I have way too many plugins installed!).

Any thoughts or help appreciated!

I don't know if this works when a drive is considered busy, but...

diskutil eject diskID

...will eject the drive with the matching diskID N via Terminal. To determine which drive is which, use diskutil list external in Terminal, and find the identifier for the target drive's name.

-rob.

I have just been testing again and diskutil unmount works when Logic Pro is running but all projects that use one's own audio have been closed.

LP does not seem to care about disk disappearance for a project that uses only Apple Loops, even if the project is unsaved!

Thanks Rob. I'll give it a go!

I think diskutil eject / would eject all disks, right?

Well that's encouraging... I'll report back if eject doesn't work.

I don't think that would work, as it's looking for a disk ID at the end, not the path to the root of all drives. A quick web search found umount, which I wasn't previously aware of. This thread talks about it, as well as some other ways to eject all drives:

umount looks like the easiest way, including a way to force-eject busy disks, but it requires sudo. There's also some code to use diskutil and cycle through all external drives, etc.

-rob.

1 Like

And when really stuck, diskutil unmount force <volumeName> -- the "Force Eject" option in Finder. And no sudo required, unlike umount.

2 Likes

This is what I've ended up with:

#!/bin/bash

# Get a list of all external drives
external_drives=$(diskutil list external | grep "disk[0-9]" | awk '{print $1}')

# Eject each external drive
for drive in $external_drives; do
    echo "Attempting to eject $drive..."
    
    # Try to forcefully unmount the drive
    diskutil unmountDisk force $drive
    if [ $? -ne 0 ]; then
        echo "Unable to eject $drive, skipping..."
    else
        echo "Successfully ejected $drive"
    fi
done

echo "All external drives have been ejected."
1 Like

Hi, @noisneil. Maybe it’s now moot, but if you don’t have this completely solved, this might help: MACRO: Time Machine Assistant, v1.1

Certainly not moot. What I would like is a reliable method to detect whether a TM backup is in progress, and if so, either wait until it completes or stop it and then wait until it's stopped. Do you think your macro might be able to do some of that?

1 Like

I looked at your macro, and I'm not sure what's unreliable about what you have there? What problems are you having with it.

I wrote a similar macro for a friend, and also used tmutil, but in this manner:

tmutil status | grep "Running = 0;"

If the status were anything other than "Running = 0," then I knew Time Machine was still doing something. But your method looks to be at least as robust, so I'm having trouble seeing where the troubles are :).

-rob.

1 Like

Yes, it does. You might need to adjust it for your workflow, but I think it has all the components you might need.

BTW, it’s really 100% AppleScript within a single macro action.

1 Like

It's partly a matter of confidence. The only time I know whether it's working is when I shut down and there's a backup in progress. I've had a few shutdowns that didn't eject the disks properly first, and it's a bit hard to know what went wrong without repeating the shutdown for testing purposes. I'm not keen on doing that too often if I can avoid it.

Have you a handle on which prompt option you choose when this happens? The only thing that leaps out is that your "Stop and Wait Until Stopped" script has a max wait of 2 minutes -- so you might be trying to eject while TM is still running.

Are you seeing any error dialogs from your "Eject" AS script? You could try logging any error messages to file instead.

And it might not be TM that's the problem -- Spotlight's a b*gger for this sort of thing, you might have a Finder window open but minimised, etc. You could try adding

sudo lsof | grep "/Volumes/<diskThatDidn'tEject>" > /Users/<shortname>/Desktop/open_files.txt

...editing sudoers so you don't need a password, to see what's blocking things.

Touch wood, the shell script version does seem to eject everything properly, including the drive that Logic was being picky about.

I suppose I'm just asking for confirmation that I'm doing the TM pause stuff correctly, as I can't really figure that out for myself due to testing restraints.

You're right that 2min probably isn't long enough to be safe. Maybe I need a KM check loop instead..?

Fast machine, SSDs -- could be fine. You can try it directly, just manually trigger a backup and try a cancel during the different phases and see how long it takes.

1 Like

I've changed things around a bit and am using KM's timeout instead:

Studio Shutdown.kmmacros (56 KB)

Macro screenshot

1 Like