2

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.

#!/bin/sh                               

WATCHED_DIR="/var/www/html/magento/media/tmp/catalog/product"

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

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.

F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
0 S     0 29137  2705  0  80   0 - 26538 wait   pts/14   00:00:00 test.sh
1 S     0 29139 29137  0  80   0 - 26538 pipe_w pts/14   00:00:00 test.sh

Does anyone know the solution?

2
  • If you have solved your problem you should add an answer and accept it. Not update the question/title.
    – Zoredache
    Feb 11, 2012 at 18:37
  • You can. Just wait a bit.
    – Zoredache
    Feb 12, 2012 at 1:10

2 Answers 2

2

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.

5
  • no, the directory is empty. but good to know
    – Christian
    Feb 11, 2012 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, 2012 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, 2012 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, 2012 at 18:21
  • good! you do not need the apache password if you launch the su command as root.
    – Olivier S
    Feb 11, 2012 at 18:23
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .