linux - How to run inotifywait continuously and run it as a cron or deamon? -
i've built shell script uses inotifywait automatically detect file changes on specific directory. when new pdf file dropped in directory script should go off , should trigger ocropus-parser execute commands on it. code:
#!/bin/sh inotifywait -m ~/desktop/pdffolder -e create -e moved_to | while read path action file; #echo "the file '$file' appeared in directory '$path' via '$action'" # check if file pdf or file type. if [ $(head -c 4 "$file") = "%pdf" ]; echo "pdf found - filename: " + $file python ocropus-parser.py $file else echo "not pdf!" fi done this works pretty when run script through terminal ./filenotifier.sh when reboot linux (ubuntu 14.04) shell no longer run , not restart after reboot. i've decided create init script starts @ boot time (i think). did copying file filenotifier.sh init.d:
sudo cp ~/desktop/pdffolder/filenotifier.sh /etc/init.d/ i've gave file correct rights:
sudo chmod 775 /etc/init.d/filenotifier.sh and i've added file update-rc.d:
sudo update-rc.d filenotifier.sh defaults however when reboot , drop pdf in folder ~/desktop/pdffolder nothing happen , seems script not go off. i'm not experienced init.d, update-rc.d , deamon i'm not sure wrong , if approach or not.
thanks, yenthe
being init-script, should add lsb header script, this:
#!/bin/sh ### begin init info # provides: filenotifier # required-start: $remote_fs $syslog # required-stop: $remote_fs $syslog # default-start: 2 3 4 5 # default-stop: 0 1 6 # short-description: # description: else ### end init info inotifywait -m ...this way, can ensure script runs when mount points available (thanks
required-start: $remote_fs). essential if home directory not on root partition.another problem in init-script you're using
~:inotifywait -m ~/desktop/pdffolder ...the
~expands current user home directory. init-scripts run root, it'll expand/root/desktop/pdffolder. use~<username>instead:inotifywait -m ~yenthe/desktop/pdffolder ...(assuming username
yenthe.)or perhaps switch user before starting (using
sudo).$filebasename without path directory. use"$path/$file"in commands:"$(head -c 4 "$path/$file")" python ocropus-parser.py "$path/$file"maybe consider using
nameinstead offile, avoid confusion.if things not working, or if in general want investigate something, remember use
ps, this:ps -ef | grep inotifywaitpstell you, example, whether script running , ifinotifywaitlaunched correct arguments.last not least: use
"$file", not$file; use"$(head -c 4 "$file")", not$(head -c 4 "$file"); useread -r, notread. these tips can save lot of headaches in future!
Comments
Post a Comment