2

I want to monitor a directory on my server for additions (or file update), and when something is added, run a php script.

I saw that there is watch, but I'm not sure exactly how to use it.

I know watch -d ls -l will track changes in the file listing, but then how do I pipe the changed file to a php script? Also, how do I watch for a file that is not new, but updated?

Can I run this alongside a config file (what directories, etc) for easy setup to end users?

switz
  • 24,384
  • 25
  • 76
  • 101
  • 1
    Related: [Folder monitoring and event triggering according to folder status in php](http://stackoverflow.com/questions/8135842/folder-monitoring-and-event-triggering-according-to-folder-status-in-php) – hakre Nov 17 '11 at 18:55

2 Answers2

2

You can use inotifywait (from inotify-tools) in a bash script.

while read file; do
    php some_script.php "$file"
done < inotifywait -e create,delete,move,modify -m . --format "%w%f" $dir

%w%f will give you the file path. If you also need the event add %e. For more format options and event names, see the manpage.

Another possibility would be incron, but that requires a system daemon.

Bluewind
  • 1,054
  • 7
  • 10
0

Every time I have used watch, it seems to consume the current console/tty. Probably not the best tool for piping into a php script.

One approach I would take is to have a php script do the directory listing, store in a DB like SQLite, etc. then compare the differences, and since it is a php script, it can pass the info off to other php scripts or have the rest of the script incorporated. You can have cron run your php script.

Tim
  • 1,840
  • 2
  • 15
  • 12