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
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