3

I have CentOS 7 with installed and running incrond.

As I understand incrond service can monitor /var/www/html/uploads/ and if a new file is uploaded - then using IN_CREATE we can do something. In my case I'd like to copy this new file to directory /var/www/html/uploads/new/

But when I try to use an incron job

/var/www/html/uploads/ IN_CREATE cp /var/www/html/uploads/new/

I have no a result.

I guess my raw example is wrong but I can't catch how to use INCRON to copy new files from monitoring directory to another location.

Thanks for any hints and ideas to try.

Serge
  • 679
  • 1
  • 9
  • 23

2 Answers2

4

The syntax of an incron entry is:

<path> <mask> <command>

If you monitor directory, then $@ contains the directory path and $# the file that triggered the event but if you monitor a file, then you have to use $@$# which contains the complete path.

In your case the entry should look like:

/var/www/html/uploads/ IN_CREATE cp $@$# /var/www/html/uploads/new/
Alexander Baltasar
  • 1,044
  • 1
  • 12
  • 25
1

I found much better solution on Stackexchange using INOTIFY.

First

yum install inotify-tools

Then create a bash script, let's say dirmonit.sh like

inotifywait -m /var/www/html/uploads/ -e create -e moved_to |
    while read path action file; do
        echo "The file '$file' appeared in directory '$path' via '$action'"
        # do something with the file
    cp /var/www/html/uploads/$file /var/www/html/uploads/new/$file
    done

and start on shell background

nohup sh /scripts/dirmonit.sh &
Community
  • 1
  • 1
Serge
  • 679
  • 1
  • 9
  • 23
  • Why is it better than incron? – Shan-x Dec 21 '16 at 14:38
  • Beginning with 0.5.11 incrond uses system() to run your command with very incomplete metachar filtering. Filenames can contain arbitrary shell metachars - you can figure it out from there. https://bugzilla.redhat.com/show_bug.cgi?id=1370316 Until incrond fixes this, you are better off rolling your own using inotify (be sure to quote everything thoroughly when using shell - I would recommend python or C instead). Which is sad - incrond was a nice idea. Note: you're ok if you can guarantee there are no nasty chars in filenames - but I still wouldn't use a root incrond script. – Stuart Gathman Aug 22 '17 at 21:45
  • For instance, the dirmonit.sh example above can do nasty things when shell metachars are in the filename - because the author forgot to quote $file . . . – Stuart Gathman Aug 22 '17 at 21:49