Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a very simple script to log when new files are created, in fact, for test purposes I told it to watch for any event.



echo "Watching directory: $WATCHED_DIR for events"
inotifywait -m -q -r "$WATCHED_DIR" --format "%|e %w%f" |
  while read resp
    echo "$resp" >> watcher.log

when i create a file within the watch directory, it reacts and logs

CREATE /var/www/html/magento/media/tmp/catalog/product/2/1/new file
OPEN /var/www/html/magento/media/tmp/catalog/product/2/1/new file
CLOSE_WRITE|CLOSE /var/www/html/magento/media/tmp/catalog/product/2/1/new file
OPEN /var/www/html/magento/media/tmp/catalog/product/2/1/new file
CLOSE_NOWRITE|CLOSE /var/www/html/magento/media/tmp/catalog/product/2/1/new file
OPEN|ISDIR /var/www/html/magento/media/tmp/catalog/product/2/1
OPEN|ISDIR /var/www/html/magento/media/tmp/catalog/product/2/1/
OPEN /var/www/html/magento/media/tmp/catalog/product/2/1/new file
CLOSE_NOWRITE|CLOSE /var/www/html/magento/media/tmp/catalog/product/2/1/new file
CLOSE_NOWRITE|CLOSE|ISDIR /var/www/html/magento/media/tmp/catalog/product/2/1
CLOSE_NOWRITE|CLOSE|ISDIR /var/www/html/magento/media/tmp/catalog/product/2/1/
OPEN /var/www/html/magento/media/tmp/catalog/product/2/1/new file
CLOSE_NOWRITE|CLOSE /var/www/html/magento/media/tmp/catalog/product/2/1/new file
OPEN /var/www/html/magento/media/tmp/catalog/product/2/1/new file
CLOSE_NOWRITE|CLOSE /var/www/html/magento/media/tmp/catalog/product/2/1/new file

but when apache creates a file, nothing is logged

The only difference I can spot is when apache creates the file, the file is owned by apache, where as when i create the file, it is owned by root.

-rwxrwxrwx. 1 apache apache 55659 Feb 11 16:46 2164.jpg
-rw-rw-r--. 1 root   root       0 Feb 11 17:02 new file

The watcher script is run as root.

0 S     0 29137  2705  0  80   0 - 26538 wait   pts/14   00:00:00
1 S     0 29139 29137  0  80   0 - 26538 pipe_w pts/14   00:00:00

Does anyone know the solution?

share|improve this question
If you have solved your problem you should add an answer and accept it. Not update the question/title. – Zoredache Feb 11 '12 at 18:37
i cant accept my own answers – Christian Feb 11 '12 at 23:02
You can. Just wait a bit. – Zoredache Feb 12 '12 at 1:10

You are using the recursive mode, which adds watch automatically for all subdirectories. The maximum amouint of inode watches per user is 8192, are there more than 8192 directories under /var/www/html/magento/media/tmp/catalog/product ?

find /var/www/html/magento/media/tmp/catalog/product -type d | wc -l

if it's greater than 8192 you can do as suggested by the man page: write to /proc/sys/fs/inotify/max_user_watches

Also, might look stupid but in data you provide, we can see that the apache file was created 16 minutes before the file created by root. Are you certain you launched your script before Feb 11, 16:46 ? Also, if you launched it just before, there is possibility that you did not receive the event because the program was still recursively creating the inode watches, and since you use the "-q" option you have no way to know when this phase has completed.

share|improve this answer
no, the directory is empty. but good to know – Christian Feb 11 '12 at 18:08
i started the script, tested it with apache, then realised it wasnt working and created my own file, hence why the apache file is first. – Christian Feb 11 '12 at 18:10
if you run something like ' su apache -c touch /var/www/html/magento/media/tmp/catalog/product/test_with_su_apache ', can you see it ? – Olivier S Feb 11 '12 at 18:19
i dont know what the apache password is. however, please see my edited question. thanks for taking the time to respond. – Christian Feb 11 '12 at 18:21
good! you do not need the apache password if you launch the su command as root. – Olivier S Feb 11 '12 at 18:23
up vote 1 down vote accepted

Seems it took a while to start recording but when it eventually did, it wasnt reporting CLOSE_WRITE, it was reporting MOVED_TO

CREATE|ISDIR /var/www/html/magento/media/tmp/catalog/product/i
OPEN|ISDIR /var/www/html/magento/media/tmp/catalog/product/i
CLOSE_NOWRITE|CLOSE|ISDIR /var/www/html/magento/media/tmp/catalog/product/i
MOVED_TO /var/www/html/magento/media/tmp/catalog/product/i/n/intelcorei5.jpg
ATTRIB /var/www/html/magento/media/tmp/catalog/product/i/n/intelcorei5.jpg
ATTRIB /var/www/html/magento/media/tmp/catalog/product/i/n/intelcorei5.jpg
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.