I talked with the ChatGPT Terminal helper, and made this script, which hopefully works.
- Open the shortcuts app and choose "Run Shell Script"
- Paste this code in and replace "MyDrive" with your SSD's actual name.
This should search for the PID on the drive, find them, disable them, then automatically attempt to eject the drive all in one button.
DRIVE="/Volumes/MyDrive"
# Find PIDs locking the drive
PIDS=$(lsof | grep "$DRIVE" | awk '{print $2}' | sort -u)
# Kill each PID
if [ -n "$PIDS" ]; then
echo "Killing processes: $PIDS"
echo "$PIDS" | xargs -I{} kill -9 {}
else
echo "No processes are locking the drive."
fi
# Attempt to eject the drive
diskutil eject "$DRIVE"
# Check if the eject was successful
if [ $? -eq 0 ]; then
echo "Drive ejected successfully."
else
# If the eject fails, prompt the user to force eject
osascript -e 'display dialog "Failed to eject the drive. Would you like to force eject?" buttons {"Cancel", "Force Eject"} default button "Force Eject"' > /dev/null
RESPONSE=$?
if [ $RESPONSE -eq 1 ]; then
echo "Force ejecting the drive..."
diskutil unmountDisk force "$DRIVE"
else
echo "User chose not to force eject."
fi
fi