September 16, 20241 yr 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 February 2, 20251 yr by Espressomatic Fixed to prevent syslog output on already-mounted shares
September 19, 20241 yr 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 February 2, 20251 yr by Espressomatic
September 26, 20241 yr 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!
January 14, 20251 yr 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!
January 28, 20251 yr 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 January 28, 20251 yr by Espressomatic
January 28, 20251 yr 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.
January 29, 20251 yr 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?
January 29, 20251 yr Author Let me review the code and the share list file... Edited January 29, 20251 yr by Espressomatic
January 29, 20251 yr 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: This is what UD puts into its mount file: 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. 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 January 29, 20251 yr by Espressomatic
January 29, 20251 yr 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.
January 29, 20251 yr 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.
January 31, 20251 yr Author Here are a couple of captures showing the Share name and Mount Point Name: Edited February 2, 20251 yr by Espressomatic
April 20, 20251 yr 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 April 20, 20251 yr by UnraidTobias
May 19, 20251 yr 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.
January 24Jan 24 I ran the script as posted and get:The formatting of the samba_mount.cfg file isn't as expected ... abortingPlease contact the developer or file a bug on the github repoAny help please?
February 12Feb 12 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 ... abortingI 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 February 12Feb 12 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.