0

I would need a script that can check files when they are getting uploaded/created on the server.

I wrote the script that checks, but I would like to make it run when a create event happens. I tried it with iwatch then with inotify but I experienced the problem, that if more files are copied at the same time inotify/iwatch only runs once or twice, but not as many times as many files are created.

So for example I run: while : ; do inotifywait --format '%f: %e' -e create /path/to/watch; done

And on an other terminal but the same server of course I enter a directory with lots of files in it and inotify only notifies me on the other terminal some times, but not all the time as it should do.

Can someone give me a hint what can cause such thing to happen?

Thanks in advance.

vdavid
  • 31
  • 1
  • 1
  • 4
  • Maybe this from the [manual](http://linux.die.net/man/7/inotify): *Note that the event queue can overflow. In this case, events are lost. Robust applications should handle the possibility of lost events gracefully.* – HBruijn Jan 08 '16 at 16:24
  • Thanks for the idea HBruijn, but it's not what I'm looking for. When I do `cat /proc/sys/fs/inotify/max_queued_events` I got `16384`, which means the program has a lot of free places in the queue for events, since I only generate some when copying 4-5 files. – vdavid Jan 11 '16 at 09:29

2 Answers2

0

There is a great tool called lsyncd. It is for synchronisation but I've used it for changing the permission for uploaded file too. Take a look at example.

You can either use it or look through the code, it uses inotify.

BR

  • Thank you for your comment Ruslan. I will have a look at it later, but first I would like to solve it Inotify based. – vdavid Jan 11 '16 at 09:55
0

I've managed to figure out what the problem was. Inotify/Iwatch could see and give my script these files matching the given events, but when the script started to process them Inotify didn't keep the files till the script finished but through them away.

So I made a primitive cache. First my script writes files in a text file as a list, and meanwhile gives this text file to another process (a module of my script) to process them. And does it with '&', so the main script doesn't wait for the module to get through the list.

vdavid
  • 31
  • 1
  • 1
  • 4