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

  • $file basename without path directory. use "$path/$file" in commands:

    "$(head -c 4 "$path/$file")" python ocropus-parser.py "$path/$file" 

    maybe consider using name instead of file, avoid confusion.

  • if things not working, or if in general want investigate something, remember use ps, this:

    ps -ef | grep inotifywait 

    ps tell you, example, whether script running , if inotifywait launched correct arguments.

  • last not least: use "$file", not $file; use "$(head -c 4 "$file")", not $(head -c 4 "$file"); use read -r, not read. these tips can save lot of headaches in future!


Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

json - Gson().fromJson(jsonResult, Myobject.class) return values in 0's -