MiniDLNA Automatic Scan - Fix

Status
Not open for further replies.

tanik1

Contributor
Joined
Mar 31, 2013
Messages
163
MiniDLNA will not automatically recognize changes in your file database for any FreeNAS system at this time. In order to work around this short coming I created this little tweak. Once there is a fix, I will update the posting to reflect which version you don't need this fix in. Of course you could just perform a manual rescan if you like, it depends how automated you want your system.

Thanks for the clarification.

Hopefully when the 9.1 is out this will be fix. but for now how would one perform a manual rescan?
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Thanks for the clarification.

Hopefully when the 9.1 is out this will be fix. but for now how would one perform a manual rescan?
Stop the MiniDLNA plugin and wait about 15 seconds for the system to settle down, then re-enable MiniDLNA plugin.
 

Jonus3000

Cadet
Joined
May 16, 2013
Messages
8
Thank you for an excellent script i am having a bit of trouble with it, I had it all setup and working fine but since i started freenas as a tester with just what i had lying around there came a point when i need to start again with proper parts. This is where my issues started with it rescanning all the time. All that i changed was the HDD everything else is the same. I am currently running FreeNAS 8.3.1-RELEASE-p2-x64, with cifs enabled it does not make a difference whether it is on or off. I have tried the script on post 56 still the same rescanning. There are 2 computers, 1 Samsung tv and 2 phones in the house which use it but never more than 1 at a time as it is just me. All of which did not previously cause an issue of rescanning.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
I'm unable to help because the information you provided is not enough for me to provide some advice other than if you only replaced your hard drive(s), you should consider removing the script and ensure the scanning stops. Then try to recreate the script.
 

Jonus3000

Cadet
Joined
May 16, 2013
Messages
8
I have removed the script (both types) and the rescanning stops upon adding it back it starts again for approx whatever the trigger delay is. I have followed the uninstall guide to start again from a fresh just can't work out could be writing to it? (None of the devices previously did.)
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Did you add any new network devices? I recommend that you isolate your equipment to see what is causing the problem. Something must be writing into the area the scanning script is looking at. If you disconnect the FreeNAS from your network, does it still happen? If yes then did you add any applications to FreeNAS? If no, start attaching network devices until you see it trigger. I'd adjust the trigger time to something small like 15 seconds for the testing period or it will take a very long time to figure it out.
 

Jonus3000

Cadet
Joined
May 16, 2013
Messages
8
Hi is there a log file to check for this as if disconnect from the network how can i see if dlna is working? Sorry for the noob questions am quite new to freenas
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Hi is there a log file to check for this as if disconnect from the network how can i see if dlna is working? Sorry for the noob questions am quite new to freenas
When you disconnect your FreeNAS computer from your network and if you watch the hard drive lights for activity, like when the MiniDLNA is rescanning, that is your indication. If the script isn't working for you then maybe you should remove it and just restart either MiniDLNA plugin whenever you need to update the database or reboot FreeNAS, either will get the job done.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
I have verified this script runs in FreeNAS 9.1.1-RELEASE. There is one quirk, the GUI for the MiniDLNA plugin, once the script is enabled well the GUI will not show the plugin as running however it actually is running. Also I discovered a new bug with FreeNAS 9.1.1 in that the shared file paths cannot have any spaces in them. I have reported this bug.
Bug #3103: dlna_1 jail share mount does not mount automatically.
 

westfighter

Cadet
Joined
Sep 13, 2013
Messages
3
Thank you for that script! It works with 9.1.1, but i have now a problem.
During the rescan my Sony TV loses the connection to FreeNAS!
Watch a movie is not possible, because every 5min it interrupts ;)
Can I fix this problem? Without reducing the update regularity?
Thank you!
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Nope, anytime there is a rescan the video stream will be interrupted. If it's doing this every 5 minutes then you must have something writing to the media directory (whatever you are sharing). Read the entire thread and there is an older script listed that will probably help you out.

What DLNA devices do you have on your network? Some can cause this problem.
 

westfighter

Cadet
Joined
Sep 13, 2013
Messages
3
Thank you for the answer! I only have the Sony TV W805 and freenas. Some PC's and Mac's for data sharing over freenas.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Try the script here at posting #56 and let me know how it goes. I'd like to address any problems as they come up.

MiniDLNA Automatic Scan - Fix

Also, have you tried Plex yet? This is a new plugin that can be added via the GUI and it has a DLNA service setup by default. I'm in the process of testing it and so far I like it a lot. I can't tell you if it automatically rescans the database automatically upon a change as I'm testing it right now but haven't had a positive result yet. But the DLNA server does seem to work fine except for when using Windoze Media Player. There is a thread on Plex already and I don't want to discuss it in this thread other than to let folks know that there is another option to MiniDLNA right now.
 

rocketjordon

Cadet
Joined
Sep 15, 2013
Messages
1
So I've been working on this and go figure, it works pretty good. The only downside I have is if you make a change while you are streaming a video, it generally will stop the streaming. I really don't consider this a downside for my use but it will be something you have to factor in. You can try it to see if you like it, if not then it's easy to disable.

Would it not be possible to prevent this by first checking the cpu usage of minidlna, and in case it is above, for example, 10% just end the script or wait 15 minutes and then check again?
 

westfighter

Cadet
Joined
Sep 13, 2013
Messages
3
Thank you for the Advise. I don't tried the script and tried Plex first.
Unfortunately, I have exactly the same problem. I have set the "Update interval of the library" to 15min but it does not.
Except I update manually.
Did I forget something or is this normal? (In which Thread can I find help?)
But thanks anyway! Plex seems to be the better solution!
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
Would it not be possible to prevent this by first checking the cpu usage of minidlna, and in case it is above, for example, 10% just end the script or wait 15 minutes and then check again?
I don't see why you couldn't adapt the script to do that if you're willing to figure it out but I doubt that will help out much, what if you hit period of time where the CPU is idle which would be quite often for my system. My advice is to figure out what is causing a change in the directories you are monitoring or don't use the script and manually restart the plugin to update your media listings.

The Plex plugin also does not automatically update, or at least I have been unable to get it to do it.
 

Sahnebar

Cadet
Joined
Mar 21, 2012
Messages
2
Hello joeschmuck,
Thank you for that script! It works fine with 9.1.1, but now i have a problem... :(
I have three Jails, when i reboot my server two of the jails won't start.
I added your script to my dlna jail, this will start, now if i stop this jail the other's will start too.
But this will be a problem, i won't start the jails manually if i reboot.
Have you any ideas what can i do?

Sani

//Edit:
I found out something,
When i delete the complete config and uninstall the "wait_on-1.1" then all jails are starting.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
The problem you are experiencing is when the script is running and waiting on "wait_on" to detect a change, well for some odd reason the operation affects other jails. I have no clue why this is.

One way around your situation is to delete the MiniDLNA plugin. Now it's not the first plugin. Now add MiniDLNA, it shoudl be the last listed plugin. All your plugins before MiniDLNA should start up normally (I hope) and them MiniDLNA will run. Of course it will not show you it's running.

So what is going on in the Plugin's ? I have no idea. This was a major change from 8.3.x and I don't like it because you would think you could create a custom plugin and make this work perfectly, nope not so. Once I figure out how to create a plugin I will create MiniDLNA using Kqueue vice a script and that should solve this problem entirely and there will be no need for a script.
 

joeschmuck

Old Man
Moderator
Joined
May 28, 2011
Messages
10,994
This is an update posting for use of this script with FreeNAS 9.1.1-Release and Minidlna 1.1.10_1.

A few things to mention here is with the updated MiniDLNA plugin we are now able to use the command "service minidlna onerestart" vice killing the snot out of the minidlna program just to ensure it dies. The second thing to mention are the problems I've encountered since FreeNAS 9.1.1-Release:
  • The jails are not operating as they use to in previous versions of FreeNAS and there is some sort of hangup going on preventing execution of subsequent jails after the dlna_1 jail starts.
  • The MiniDLNA service doesn't appear in the GUI (ON/OFF) to start even though it is running. I don't know what is going on but my efforts to troubleshoot the problem have not met with good results.
  • If MiniDLNA is either your only jail or your last jail, you can automate the script as in posting #1 of this thread without it affecting the operation of your system however you must keep this in mind if you do add a jail in the future, and realize the GUI will not show MiniDLNA running even though it is running.
So because there is some weird thing going on with the jail I though I should write another entry and show the current code I'm using and how I run it.

I'm asking for suggestions on how to make this script run properly as I have not figured a way that works automatically and properly since FreeNAS 9.1.

The script is virtually identical to the script in the first posting with the exception of the way we kill and restart the minidlna program. (Script posted below)

To run the script I cannot do this automatically so I
Code:
chmod -x scanmedia
so it cannot run without being told to specifically and removed it being enabled in the rc.conf file. You can do this anyway you like, it's just what I did because I am still working the problem.

To run the script I have resorted to doing this manually by typing in the following at the shell and only do it once as you can end up running multiple instances of the script:
Code:
jexec dlna_1 sh ./etc/rc.d/scanmedia &
You should get a numerical response.

This will run the scanmedia script in the background.

My current version of this script is:
Code:
    #!/bin/sh
    # File name 'scanmedia'
    # Place this file into /etc/rc.d
    # Edit /etc/rc.conf to include scanmedia_enable="YES"
    # THIS VERSION IS FOR "MINIDLNA 1.1.10_1" and uses the 'restart' parameter.
   
    # Things to know...  The purpose of this script is to trigger a rescan of the
    # media for the minidlna plugin.  This uses the "wait_on" command which
    # although works, it has it's limitations:
    #  1) It it rescans on the detection of a change, not the completion of a change.
    #  2) It has a limitation of the directory string length so I have implemented
    #    an alternate method for scanning however it does actively read the drives
    #    so if you wanted your drives to sleep, this method will prevent sleeping
    #    from occurring and I strongly recommend you work on your directory structure.
   
   
    . /etc/rc.subr
   
    name="scanmedia"
    rcvar=scanmedia_enable
   
    PATH="$PATH:/usr/local/bin"
   
    start_cmd="${name}_start"
    stop_cmd=":"
   
    load_rc_config $name
    eval "${rcvar}=\${${rcvar}:-'NO'}"
   
    # Variables used in this script...
   
    # RedAlert = Maximum string length wait_on can handle
    RedAlert=261649
   
    # YellowAlert = User selected warning point.  Looking to use this to enable the
    #  flashing warning light in the GUI.
    YellowAlert=255000
   
    # TriggeredDelay = The amount of time in seconds to delay rescanning from the point of a
    # change being detected.  The default is 300 seconds (5 minutes).  Change to your liking
    # and think of this as how long it takes you to write a large file (movie) to your NAS.
    TriggeredDelay=300
   
    # AlternateScanRate = The amount of time in seconds between polling the media for changes.
    # Default value is 1800 seconds (30 minutes).  Change to your liking.
    AlternateScanRate=1800
   
    # ScanMode = Normal or Alternate method.  Default is '0' for Normal Mode.  This changes
    # in the script based on the actual directory string length vs the RedAlert value.
    ScanMode=0
   
    # MediaLocation = The location your want to scan for your media.  The default is "/media".
    MediaLocation="/media"
   
    scanmedia_normal()
    {
    while [ "$ScanMode" -eq "0" ] ; do
   
    # First lets locate all the subdirectories.  Since the minidlna plugin only
    # allows one path for media we will use the path defined above (default=/media)
   
    directories=$(find $MediaLocation -type d -print0 | xargs -0 -I % echo -n '"%" ')
   
    # Check on maximum length of directory string.
   
    if [ ${#directories} -gt $YellowAlert ];
      then
      if [ ${#directories} -gt $RedAlert ];
      then
      date > /minidlnaerror.log
      echo "RED ALERT! You have exceeded the maximum directory string length of 261649 and you are currently at" >> /minidlnaerror.log
      echo ${#directories}".  The alternate method is in use and you need to shorten the directory names or rearange your folders" >> /minidlnaerror.log
      echo "to restore the normal scanning method." >> /minidlnaerror.log
    # Change from normal scan mode to alternate scan mode.
      ScanMode=1
      else
      date > /minidlnaerror.log
      echo "YELLOW ALERT! The maximum directory string length is 261649 and you are currently at" >> /minidlnaerror.log
      echo ${#directories}".  You should shorten the directory names or rearange your folders" >> /minidlnaerror.log
      echo "to remove this warning." >> /minidlnaerror.log
      fi
    fi
   
    if [ "$ScanMode" -eq "0" ]
    then
   
    # Wait_on will trigger on any event which writes to the directories listed.
   
    csh -c "wait_on -w $directories"
   
    # Now let's call the rescanning routine
    scanmedia_rescan
   
    fi
    done
    }
   
    scanmedia_rescan()
    {
    # This routine pauses for the user specified delay before restarting the minidlna service.
   
      sleep $TriggeredDelay
      service minidlna onerestart
    }
   
    scanmedia_alternate()
    {
    while [ "$ScanMode" -eq "1" ] ; do
   
    #Lets check to see if we can go back to normal_scan mode.
    directories=$(find $MediaLocation -type d -print0 | xargs -0 -I % echo -n '"%" ')
    if [ ${#directories} -lt $RedAlert ]
    then
    ScanMode=0
    fi
   
    # We list all the files and save as a file in memory, hopefully the file doesn't get too
    # big to cause an issue.  If it does then remove the '/var/' from the below lines.
   
    ls -RlT $MediaLocation > /var/altscan1.dat
    sleep $AlternateScanRate
    ls -RlT $MediaLocation > /var/altscan2.dat
   
    # diff return 0 if files are identical, 1 if they are different, 2 if one of the files cannot open.
    diff -q /var/altscan1.dat /var/altscan2.dat
   
    # We don't really care about cases 0 and 2 but it's nice to have for troubleshooting. They
    # are commented out right now.
    case "$?" in
    #"0") echo "Files Identical";;
    "1") scanmedia_rescan;;
    #"2") echo "Failed to open one of the files";;
    esac
   
    done
    }
   
    scanmedia_start()
    {
    while :; do
    if [ "$ScanMode" -eq "0" ]
    then
    scanmedia_normal
    else
    scanmedia_alternate
    fi
    done
    }
   
    scanmedia_start
   
    run_rc_command "$1"

So any help would be appreciated as I fear I've gone about as far as I can go for now.

-Mark
 

twisted

Dabbler
Joined
Sep 12, 2012
Messages
23
Just a quick question about this script. Based on the timings will this prevent drives from going into standby?
 
Status
Not open for further replies.
Top