Jump to content

Workaround for Real Time Monitor on Synology SMB/NFS Share


pachi81

Recommended Posts

pachi81

Hi,

after moving my Emby Server from Synology Diskstation to Raspberry Pi with DietPI, the real time monitor on my mounted network shares does not work. On Synology I had no problems, but with the mounted shares on my Raspberry I did not get it to work. I tried to mount my Synology drive with SMB and NFS, but no success. I searched for a solution mainly in this forum, but no luck. 

So I found another solution/workaround for this problem: a script running on my Synology NAS, which triggers a library refresh task, if a file has created, changed or removed in my media folder:

1. install inotify_tools

Install from synocommunity:

2. Create script

Create script such like this:

#!/bin/bash

# check for mkv and ts files which have at least a character or a bracket
# NOTE: the PATTERN does not work in --include param (don´t know why...)

set -e -u

trap ctrl_c INT

function ctrl_c() {
    echo [$(date)] Monitoring media files canceled  >> $LOGFILE
    exit
}

WAIT_SEC=60
# add additional file extension here and to --include pattern!
PATTERN="[a-zA-Z\(\) ].*\.(mkv|ts)$"
# adjust the IP and the api-key to your settings!
COMMAND="curl --data '' http://192.168.xxx.xxx:8096/Emby/Library/Refresh?api_key=<api-key>"
LOGFILE="/volume1/media/notify.log"

echo [$(date)] Start monitoring media files  >> $LOGFILE

while true
do
	while read directory action file; do
		echo "[$(date)] The file '$file' appeared in directory '$directory' via '$action'" >> $LOGFILE
	    if [[ "$file" =~ $PATTERN ]]; then
	    	# wait for other changes for not re-trigger during the sleep time
	        echo [$(date)] Trigger scan in $WAIT_SEC seconds  >> $LOGFILE
	        sleep $WAIT_SEC
	        echo [$(date -Is)] Trigger library refresh  >> $LOGFILE
			$COMMAND &>> $LOGFILE
			break
	    fi
	done < <(inotifywait -q -m --include "\.(mkv|ts)$" -r -e create -e moved_to -e delete /volume1/media/)
	echo [$(date)] trigger done, wait for the next change >> $LOGFILE
done

echo [$(date)] Stop monitoring media files  >> $LOGFILE

The script can be easier, because I wanted to exclude files, which only have numbers. So if you don´t need it, you can remove the part with the PATTERN or change the regex (I´m not a regex expert, but it works like expected...). The wait time is also optional or can be de-/increased...

3. Create Task to run script on startup

Create triggered task for startup in Synology which is calling this script. I would suggest to test it before... ;-)

  • Like 1
Link to comment
Share on other sites

Hello pachi81,

** This is an auto reply **

Please wait for someone from staff support or our members to reply to you.

It's recommended to provide more info, as it explain in this thread:

Thank you.

Emby Team

Link to comment
Share on other sites

pachi81

I updated the script for better usage:

#!/bin/bash

# check for mkv and ts files which have at least a character or a bracket
# important some PATTERN does not work in --include param (don´t know why...)

### Configuration:
# list of file extension separate by |
FILEEXTENSIONS="mkv|ts|mp4"
# special filename regex pattern (without extension!!!) .* for all
FILENAME=".*"
# folders to monitor (recursive) separate by space " "
MONITORDIR="/volume1/Media/"
# logfilename
LOGFILE="/volume1/Media/notify.log"
# emby server address with port
EMBYSERVER="192.168.xxx.x:8096"
# emby API key
API_KEY="<replace with your API key!>"
# time to wait before trigger library scan in seconds
WAIT_SEC=60
### End of configuration 

# config variables (DO NOT CHANGE!)
INCLUDE_PATTERN="\.(${FILEEXTENSIONS})$"
FILE_PATTERN="${FILENAME}${INCLUDE_PATTERN}"
COMMAND="curl --data '' http://${EMBYSERVER}/Emby/Library/Refresh?api_key=${API_KEY}"

##### start of the script
set -e -u

echo [$(date)] Start monitoring media files > $LOGFILE

while true
do
    while read directory action file; do
        echo "[$(date)] The file '$file' appeared in directory '$directory' via '$action'" >> $LOGFILE
        if [[ "$file" =~ $FILE_PATTERN ]]; then
          # wait for other changes for not re-trigger during the sleep time
            echo [$(date)] Trigger scan in $WAIT_SEC seconds >> $LOGFILE
            sleep $WAIT_SEC
            echo [$(date)] Trigger library refresh  >> $LOGFILE
            $COMMAND &>> $LOGFILE
            break
        fi
    done < <(inotifywait -q -m --include "$INCLUDE_PATTERN" -r -e create -e moved_to -e delete $MONITORDIR)
    echo [$(date)] trigger done, wait for the next change >> $LOGFILE
done

echo [$(date)] Stop monitoring media files >> $LOGFILE

You only have to change the Configuration part.

 

(After the topic has moved, I could not edit my first post...)

Edited by pachi81
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...