Keeping a SMB share mounted on macOS (version 2)

A more robust version of my original persistent-SMB-mount maintenance script.

⚠️
This post was automatically migrated from my old blogging software, and I have not reviewed it for problems yet. Please contact me if you notice any important issues.

Since my first post on this topic, I've iterated on the SMB mount maintenance script; in particular, it now gracefully handles the case where macOS decides that "this time, for no particular reason, I'm going to mount this at /Volumes/general-1 instead of /Volumes/general."

This maintenance script is the only thing that's changed, so check out the original post for instructions on initial setup for the mount and the launchd job.

~/code/maintain-general-smb-mount.sh:

#!/usr/bin/env bash
set -u

do_mount() {
	if osascript -e 'mount volume "smb://cdzombak@jetstream.dzhome/general"'; then
		exit 0
	else
		exit 1
	fi
}

MOUNT_DIR=$(mount | grep 'cdzombak@jetstream.dzhome/general' | cut -d' ' -f3)
if [ -z "$MOUNT_DIR" ]; then do_mount; fi
[ -d "$MOUNT_DIR" ] || exit 1
[ -f "$MOUNT_DIR"/.liveness.txt ] || exit 1

This updated script:

  1. Checks whether the mount exists. If so, it gets the mount's path (in /Volumes). If not, it attempts to mount the volume.
  2. Checks that the mount's path exists and that the file .liveness.txt exists in it. If not, it exits with an error (indicating that Uptime Kuma should notify me about the problem).