Hi, tnx for this piece of code, is really useful!
I'm trying to watch a dir and launch an external sh script with the full path of the file. I've searched and adapted various piece of code, this is the result, the name for this file is monitor.py
import pyinotify
import sys
import os.path
from subprocess import call
import signal
log_file = open("/var/log/monitor.log", "a")
def log(text):
log_file.write(text + "\n")
log_file.flush()
def signal_handler(signal, frame):
log("Fine Monitoraggio Cartella Vari")
sys.exit(0)
log("Avvio Monitoraggio Cartella Vari")
signal.signal(signal.SIGTERM, signal_handler)
allowed_exts = ["mp4", "flv", "avi", "divx", "mpg", "mpeg", "mkv"]
wm = pyinotify.WatchManager() # Watch Manager
mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_MOVED_TO # watched events
class EventHandler(pyinotify.ProcessEvent):
def init(self):
self.modified_files = set()
def process_IN_MOVED_TO(self, event):
self.do_index_command(event)
def process_IN_CLOSE_WRITE(self, event):
self.do_index_command(event)
def do_index_command(self, event):
if self.is_allowed_path(event.pathname, event.dir):
log("Analizzo %s " % (event.pathname))
call(["/home/xbmc/scripts/filebot_script.sh", event.pathname])
# Remove from list of modified files.
try:
self.modified_files.remove(event.pathname)
except KeyError, e:
# Don't care.
pass
else:
log("%s non e' da monitorizzare!" % event.pathname)
def is_allowed_path(self, filename, is_dir):
# Don't check the extension for directories
if not is_dir:
ext = os.path.splitext(filename)[1][1:].lower()
if ext not in allowed_exts:
return False
if filename.find("@eaDir") > 0:
return False
return True
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wdd = wm.add_watch(["/media/TwoTera/Download/vari"], mask, rec=True, auto_add=True)
try:
notifier.loop(daemonize=True, pid_file='/var/run/monitor.pid')
except pyinotify.NotifierError, err:
print >> sys.stderr, err
the filebot_script.sh take the full path of the file and use it to make his stuff :)
filebot also "make" his own log, so if I want to check the filebot activity I watch his log
The "problem", I think that is only my mistake, is that:
If I launch the script through the command line "python monitor.py" all works fine
Now I also found a init.d script to run the script at boot, here the script:
!/bin/sh
case "$1" in
start|"")
start the monitoring daemon
python /home/xbmc/scripts/monitoraggio_file.py
;;
restart|reload|force-reload)
echo "Error: argument '$1' not supported" >&2
exit 3
;;
stop)
kill cat /var/run/monitor.pid
;;
*)
echo "Usage: fileswatch.sh [start|stop]" >&2
exit 3
;;
esac
Now, If I run the script, monitor.py works, but filebot don't make any log!
Any idea why?
Tnx, Jo