Post Reply 
Feature Request: Re-Scan/inotify
16-07-2012, 11:26
Post: #14
RE: Feature Request: Re-Scan/inotify
new version of the script:
Code:
#!/bin/sh
pdir=${0%`basename $0`}
lck_file=$pdir/`basename $0`.lck
log_file=$pdir/`basename $0`.log
ino_lck_file=$pdir/`basename $0`.inotify.lck
conf_file=$pdir/`basename $0`.conf

if [ -f $conf_file ]; then
        source $conf_file
else
        echo "ERROR: Can't locate $conf_file"
        exit 1
fi

# ------------------------------------------------------------
# Am I Running
# ------------------------------------------------------------
if [ -f "${lck_file}" ]; then

  # The file exists so read the PID
  # to see if it is still running  
  MYPID=`head -n 1 "${lck_file}"`  

  TEST_RUNNING=`ps -p ${MYPID} | grep ${MYPID}`

  if [ -z "${TEST_RUNNING}" ]; then
    # The process is not running  
    # Echo current PID into lock file
    echo "Not running"   >>${log_file}
    echo $$ > "${lck_file}"
  else
    echo "`basename $0` is already running [${MYPID}]"  >>${log_file}
    echo "`basename $0` is already running [${MYPID}]"
    exit 0
  fi

else
#  echo "Not running"
  echo $$ > "${lck_file}"
fi

echo "*********************************" > ${log_file}
echo "`date`" >> ${log_file}
echo "Main process started : $PID" >>${log_file}

restartminim() {
        while true; do
                if [ -e "$ino_lck_file" ] ; then
                        if test `find "$ino_lck_file" -mmin +1`
                        then
                                echo "Restarting minim at `date`" >>${log_file}
                                $pdir/restartminim.exp 2>&1 >>${log_file}
                                rm $ino_lck_file
                        fi
                fi
                sleep 30
        done
}

myinotifywait() {
        while eval inotifywait -q -r -e close_write,move,delete $watch_dir >>${log_file}; do
                touch $ino_lck_file
        done
}

myinotifywait&
pid1=$!
echo "Myinotifywait spawned: $pid1" >>${log_file}

restartminim&
pid2=$!
echo "Restartminim spawned : $pid2" >>${log_file}

# Kill all sub-processes
trap "kill 0" SIGINT SIGTERM EXIT
wait

echo "finish." >>${log_file}

the config file has just one line/variable:
Code:
watch_dir="/media/upnp"
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Feature Request: Re-Scan/inotify - KoS - 16-07-2012 11:26

Forum Jump:


User(s) browsing this thread: 1 Guest(s)