switz
switz

Reputation: 25188

Monitor directory and then run PHP script

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?

Upvotes: 2

Views: 2307

Answers (2)

Bluewind
Bluewind

Reputation: 1064

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.

Upvotes: 2

Tim
Tim

Reputation: 1899

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.

Upvotes: 0

Related Questions