Skip to content
View in the app

A better way to browse. Learn more.

Unraid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[Script] Unassigned Devices Share Re-Mounter (SMB and NFS)

Featured Replies

Keep your UD shares up all the time.

 

The script will mount any unmounted shares that have been set to "Automount" by Unassigned Devices. Add it to User Scripts and set a custom schedule to run it every n minutes.

 

Remote shares set up in Unassigned Devices as Automount are only brought online when the system is first started. If the shares become unmounted for any reason, such as rebooting their server(s), they remain unavailble, potentially causing problems for apps/containers where they're referenced, such as Plex/Jellyfin, etc.

 

This script checks which shares are currently unmounted, verifies the server is available, then uses Unassigned Devices own mount function to reactivate them.

 

EDITED: version 1.6 (Feb 2 2025)

 

#!/bin/bash

##================================================================================================
##
##		Unassigned Devices Share Re-Mounter - Espressomatic @Unraid Forums - 08 Aug 2024 - 2 Feb 2025
##		                                    - inspired by Marc Gutt @Unraid Forums - 20 Nov 2020
##		version 1.6
##
##		Keep Unassigned Devices' "Automount" remote shares mounted
##
##================================================================================================
#
#	Info
#	----
#
#	Tested on Unraid versions 6.12 and 7.0
#
#	Remote shares added to Unassigned Devices as Automount are only brought online when the system
#	is first started. If the shares become unmounted for any reason, such as rebooting their
#	server(s), they remain unavailble, potentially causing problems for apps/containers where
#	they're referenced, such as Plex/Jellyfin, etc.
#
#	This script checks which shares are currently unmounted, verifies the server is available,
#	then uses Unassigned Devices own mount function to reactivate them.
#
#	When scheduled as instructed below, the script will run every 5 minutues.
#
#
#	Installation
#	------------
#
#	- Install "User Scripts" plugin on each Unraid Server that will run this script
#
#	- Create a new script named "Re-Mount Unassigned Devices Remote Shares"
#
#	- Click the gear icon next to the script, click Edit Description, select all the text and replace with
#		"Mount unmounted shares if they're set to Automount" - click the green check-mark icon
#
#	- Click the gear icon, click Edit Script and Copy and Paste this script
#		into the editor - Click the Save Changes button
#
#	- There is no need to edit nor make changes to the script
#
#	- Click the Run Script button to test - shares are checked and mounted as needed
#
#	- Click the Schedule button for the script and select a custom schedule
#
#	- Paste "*/5 * * * *" into the text box to the right, excluding quotes
#
#	- Click the Apply button at the bottom
#
##================================================================================================
##
##================================================================================================
##------------------------------------------------------------------------------------------------
## The Business	- you shouldn't need to make edits below for typical use
##------------------------------------------------------------------------------------------------

## Make script race condition safe
#
if [[ -d "/tmp/${0///}" ]] || ! mkdir "/tmp/${0///}"; then exit 1; fi; trap 'rmdir "/tmp/${0///}"' EXIT;

## Read Unassigned Devices mount list for remote shares (/tmp/unassigned.devices/config/samba_mount.cfg)
#
printf "\n\nStarting...\n\n"

cat "/tmp/unassigned.devices/config/samba_mount.cfg" | while read line
do

	## Process a server entry when found ( lines starting with "[" )
	#
	if [[ $line == [* ]];then

		check_server=$line
		## Move to the next line - share protocol
		#
		read line

		## Get the share type - SMB or NFS (needed later for mounting format)
		#
		if [[ $line == "protocol"* ]]; then
			share_type=$(cut -d'"' -f\2 <<<"$line")
			if [[ $share_type != "SMB" ]] && [[ $share_type != "NFS" ]]; then
				printf "Not NFS or SMB ... skipping\n\n"
				continue
			fi
		else
		    printf "The server protocol wasn't read where expected ... skipping\n\n"
			continue
		fi

		## Get the FQDN/ip of the server to later check if it's online
		#
		server_ip=""
		mountpoint=""
		read line
        
		if [[ $line == "ip"* ]]; then
			server_ip=$(cut -d'"' -f\2 <<<"$line")
		else
		    printf "The server IP/FQDN wasn't read where expected ... skipping\n\n"
			continue
		fi

		## Check Unassigned Devices Automount setting for each share
		#
		while read line
		do
			if [[ $line == "automount"* ]]; then
				break
			## If the mount point has been renamed in the Unassigned Devices UI, take note of it
			#
			elif [[ $line == "mountpoint"* ]]; then
				mountpoint=$(cut -d'"' -f\2 <<<"$line")
			## If a duplicate key is read before the automount, then stop the script
			#
			elif [[ $line == [* ]] || [[ $line == "protocol"* ]] || [[ $line == "ip"* ]]; then
			    printf "The formatting of the samba_mount.cfg file isn't as expected ... aborting\n\n"
			    printf "Please contact the developer or file a bug on the github repo.\n\n"
				exit
			fi
		done

		##---------------------------------------------------------------------------------------------
		## If this is an Automount share, check its type and whether it needs to be re-mounted
		#
		if [[ $line == "automount"*"yes"* ]]; then

		  	## Construct SMB Share Name/Path
		  	#
			if [[ $share_type == "SMB" ]]; then

				server_name=${check_server##*//}
				server_name=${server_name%/*}

			## Construct NFS Share Name/Path
			#
			else
				
				server_name=${check_server##*[}
				server_name=${server_name%%:*}

				share_path=${check_server#*:}
				share_path=${share_path%]*}

			fi

			share_name=${check_server##*/}
			share_name=${share_name%%]*}

			## If share has been renamed in Unassigned Devices UI, use that name instead of original source
			#
			if [[ $mountpoint == "" ]]; then
				mount_name="${server_name}_${share_name}"
			else
				mount_name="${mountpoint}"
			fi

		  	## Construct mount path, SMB: "//SERVER/sharename" or NFS: "SERVER:/mnt/some_pool/sharename"
		  	#
			if [[ $share_type == "SMB" ]]; then
				mount_path="//${server_name}/${share_name}"
			else # NFS
				mount_path="${server_name}:${share_path}"
			fi

			## Mount the share using constructed name & path if not mounted & server is online
			#
			if grep -qs "$mount_name" /proc/mounts; then
				printf "The $share_type share $mount_name is already mounted\n\n"
				check_server=""
			else
				ping -c 1 -W 1 $server_ip > /dev/null 2>&1
				if [[ $? -eq 0 ]]; then
					printf "Mounting $share_type share $mount_path as ${mount_name} ... "
					/usr/bin/nice /var/local/overlay/usr/local/sbin/rc.unassigned mount "$mount_path"
					printf "\n"
				fi
			fi
		fi
		#
		## End if Automount Share
		##---------------------------------------------------------------------------------------------
	fi
done

printf "\n...Finished\n\n"

##================================================================================================

 

Edited by Espressomatic
Fixed to prevent syslog output on already-mounted shares

  • Author

Updated to

 

Version 1.1

  - now using "ip" (FQDN) of the server instead of derived "short name" to check if it's online - in case MDNS doesn't resolve the server name without domain.tld

 

Version 1.2

 - ?

 

Version 1.3

 - Check for already-mounted share in /proc/mounts instead of using the mount command to prevent unnecessary syslog messages (and it's potentially safer)

 

Version 1.4

 - Simplified by combining previous duplicate code used for mounting SMB and NFS shares

 

Version 1.5

 - Simplified by combining more duplicate code

 - Get the custom "mountpoint" if the share has been renamed in UD UI - this fixes an error display on checking if a renamed share is already mounted

 

Version 1.6

 - Minor typo corrections to output text and comments, plus minor reformatting of comment blocks

 

 

Edited by Espressomatic

Almost perfect timing, been looking for a way to do this for a bit now since my backup machine is on a power schedule.  Thanks a bunch!

  • 3 months later...

This is perfect timing for me also. Just found my off-site backup wasn't working for weeks as the remote server network went down for a few hours and it never remounted.

 

I notice it attempts to mount regardless if the share is already mounted or not. Would it be possible to do some checks to see if it is already mounted before sending the mount command again? The reason for this is my syslog now has Remote Share xxx is already mounted every 5 minutes

 

Thanks again for your work!

  • 2 weeks later...
  • Author
On 1/13/2025 at 11:42 PM, flybrys said:

I notice it attempts to mount regardless if the share is already mounted or not

 

Let me see what I can do. It's checking now, but the check needs to be reworked to not show those messages.

 

 

 

 

Edited by Espressomatic

  • Author

OK, fixed. I simplified the way I checked for mounted shares. I was previously using the mount command to check/test which caused that output.

Instead of hardcoding the mount name as "${server_name}_${share_name}", this doesn't work for those who change the mount names from the default. Can it use the mountpoint found in samba_mount.cfg instead?

  • Author

Let me review the code and the share list file...

 

 

Edited by Espressomatic

  • Author
2 hours ago, bobokun said:

Instead of hardcoding the mount name as "${server_name}_${share_name}", this doesn't work for those who change the mount names from the default. Can it use the mountpoint found in samba_mount.cfg instead?

 

You're going to have to give me an example so I can take a look into the specifics. The way it's done now isn't "hard coded" but uses the info that UD puts into its mount list. The content pulled from that list is completely variable, and if UD ever changes it, this script will continue to work. It doesn't care what the names are.

 

I  just tested making a new mount and then renaming the mount point (BIG-DL) like so:

 

image.png.6531c320c9bf85ed0b86652f199570ea.png

 

This is what UD puts into its mount file:

 

image.png.697059f06a6f0d49266880746771da85.png

 

I use the IP and path to construct what UD lists under the "Source" in the UI, which works regardless of what the mount point is named.

 

image.png.6051ddd080e5e7ec56d045ce8f61c434.png

 

I did find ONE issue with this, given the recent changes I've made. When checking if the mount already exists, the "Source" isn't the same as the mountpoint, so it thinks the share isn't mounted. Then trying to mount it again fails. I can search additionally for mountpoint and then put that in a variable so the output above can read "as BIG-DL" and the mount check should work.

 

 

 

 

Edited by Espressomatic

 

image.png.3334817e7691e1a01edb52b4fe205072.png

 

So I did test it and it does remount correctly but afterwards when you try to run it again it shows failure even though it's already mounted.

image.png.612b765bf0ebfa0e952c9f9c89461219.png

  • Author

OK, that's the issue I outlined above.  Almost done with a fix - and it'll also display the renamed mountpoint in its output.

 

 

  • Author

All fixed and posted above.  Version 1.5

 

 

  • Author

Here are a couple of captures showing the Share name and Mount Point Name:

 

image.png.b281466c02a9040c70c1bbcbde340b27.png

 

 

image.png.2178a156ba045327024d29117915b609.png

 

Edited by Espressomatic

  • 2 months later...

Can you please also add the auto-unmount option? If a device goes offline and stays offline, to the script as well? For Unraid7.

 

Cause in my case, UA, does NOT un- mount the device if it goes offline and stays of line...

Edited by UnraidTobias

  • 5 weeks later...
  • Author
On 4/19/2025 at 8:14 PM, UnraidTobias said:

Can you please also add the auto-unmount option? If a device goes offline and stays offline, to the script as well? For Unraid7.

 

Cause in my case, UA, does NOT un- mount the device if it goes offline and stays of line...

 

Didn't see this until today.  Let me take a look at this and replicate it here to see how best to handle it.

 

 

  • 2 weeks later...
  • 7 months later...

I ran the script as posted and get:

The formatting of the samba_mount.cfg file isn't as expected ... aborting

Please contact the developer or file a bug on the github repo

Any help please?

  • 3 weeks later...
  • Author
On 1/24/2026 at 9:08 AM, Rod25 said:

I ran the script as posted and get:

The formatting of the samba_mount.cfg file isn't as expected ... aborting

I haven't looked at this in a long time, but I run it on a regular schedule for all 4 of my current servers.

What version of Unassigned Devices do you have installed?

The condition that creates that error is pretty straight forward, the script is looking for duplicate keys in your cfg, which indicates some invalid content.

If you can post your cfg it should be easy to identify the problem.

Edited by Espressomatic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.