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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.