I've got a Mac Mini M1 and an older 2014 Intel Mac Mini, updated to the latest possible macOS they can support, and both connected to the same network router over wired ethernet.
The 2014 Intel Mac Mini is my NAS, with USB hard drives being shared across the network as SMB shares. Those network drives are mounted by my Mac Mini M1 (which appear on the desktop as icons of blue drives with three little people holding hands).
An ongoing issue I've been having with my Mac Mini M1 is that it always eventually DROPS the mounted network drives after some time. This seems to happen while the Mac Mini M1 has been sleeping/locked for maybe 8+ hours. This has been happening since Big Sur. Sometimes it can stay mounted for a few days, other times its shorter-- it's quite unpredictable.
I just want these SMB shares to reliably stay mounted 24/7. Is there a fix to make this so?
To automatically run the script when the SMB share becomes unmounted, you can use a combination of a `launchd` plist and a script to check the status of the mounted share. `launchd` is a system service manager on macOS that allows you to schedule and manage background tasks.
Here's how you can do it:
1. Create the script that checks if the share is mounted. Save this script into a file named `check_smb_mount.sh`:
Bash:
#!/bin/bash
SMB_SERVER="SERVER_ADDRESS"
SMB_SHARE="SHARE_NAME"
USERNAME="USERNAME"
MOUNT_POINT="/path/to/mount/point" # Replace this with your desired local mount point
# Check if the mount point directory exists
if [ ! -d "$MOUNT_POINT" ]; then
echo "Mount point not found: $MOUNT_POINT"
exit 1
fi
# Function to mount the SMB share
mount_smb_share() {
# Call the mount_smb_share.sh script
/path/to/mount_smb_share.sh
}
# Check if the SMB share is already mounted, and decide whether to mount
if mount | grep -q "//$USERNAME@$SMB_SERVER/$SMB_SHARE"; then
echo "SMB share is already mounted at $MOUNT_POINT"
else
echo "SMB share is not mounted. Mounting..."
mount_smb_share
fi
2. Create the `mount_smb_share.sh` script to handle mounting and unmounting of the share. Save this script into a file named `mount_smb_share.sh`:
Bash:
#!/bin/bash
# Define the SMB share details
SMB_SERVER="SERVER_ADDRESS"
SMB_SHARE="SHARE_NAME"
USERNAME="USERNAME"
PASSWORD="PASSWORD"
MOUNT_POINT="/path/to/mount/point" # Replace this with your desired local mount point
# Function to mount the SMB share
mount_smb_share() {
# Check if the mount point directory exists, if not create it
if [ ! -d "$MOUNT_POINT" ]; then
mkdir -p "$MOUNT_POINT"
fi
# Mount the SMB share
mount -t smbfs "//$USERNAME:$PASSWORD@$SMB_SERVER/$SMB_SHARE" "$MOUNT_POINT"
# Check if the mount was successful
if [ $? -eq 0 ]; then
echo "SMB share mounted successfully at $MOUNT_POINT"
else
echo "Failed to mount SMB share"
fi
}
# Function to unmount the SMB share
unmount_smb_share() {
umount "$MOUNT_POINT"
# Check if the unmount was successful
if [ $? -eq 0 ]; then
echo "SMB share unmounted from $MOUNT_POINT"
else
echo "Failed to unmount SMB share"
fi
}
# Check if the SMB share is already mounted, and decide whether to mount or unmount
if mount | grep -q "//$USERNAME@$SMB_SERVER/$SMB_SHARE"; then
echo "SMB share is already mounted at $MOUNT_POINT"
unmount_smb_share
else
echo "SMB share is not mounted. Mounting..."
mount_smb_share
fi
3. Make both scripts executable:
Bash:
chmod +x check_smb_mount.sh
chmod +x mount_smb_share.sh
4. Create a `launchd` plist file to schedule the `check_smb_mount.sh` script to run periodically. Save the following content into a file named `com.example.checksmb.plist`. Replace `com.example.checksmb` with a unique reverse-domain name for your script:
XML:
<?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">
<dict>
<key>Label</key>
<string>com.example.checksmb</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/check_smb_mount.sh</string>
</array>
<key>StartInterval</key>
<integer>60</integer> <!-- Interval in seconds (e.g., 60 seconds = 1 minute) -->
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
5. Move the `com.example.checksmb.plist` file to the `~/Library/LaunchAgents/` directory:
Bash:
mv com.example.checksmb.plist ~/Library/LaunchAgents/
6. Load the `launchd` plist to activate the scheduled task:
Bash:
launchctl load ~/Library/LaunchAgents/com.example.checksmb.plist
The script `check_smb_mount.sh` will now run automatically every 60 seconds (you can adjust the interval as needed) and check if the SMB share is mounted. If it's not mounted, it will run the `mount_smb_share.sh` script to attempt to mount it. If it's already mounted, it will unmount it and then mount it again. The unmounting and remounting ensure that the share is reconnected if the connection was lost or interrupted.