Вопрос

I use inotifywait to watch a folder for new files, and send the path of the file to a php script to process it when "close_write" is detected.

The command looks like this:

inotifywait -e close_write --format '%w%f' -m -r /path/do/dir | while read LINE; do php /home/scripts/watcher.php $LINE; done

When its executed in ssh, it works perfectly. When I wrap it in a exec() or system() functions in php, in order to run it as a daemon with supervisor, it doesn't pass the 2nd arg to the watcher.php script that usually contains the absolute path of the file that triggered inotifywait.

exec() doesn't even trigger the script at all, while passthru() and system() actually hit watcher.php but with no args other than the first one, which contains the path of the script itself.

When the wrapper script is executed, to bein watching the folder, it gives the output from ionotifywait:

Setting up watches.  Beware: since -r was given, this may take a while!
Watches established.

When a new file is uploaded, nothing happens. watcher.php is not triggered.

When I use passthru() or system() and upload a file, it hits watcher.php, which does this at the top of the file: print_r($argv);

passthru("inotifywait -e close_write --format '%w%f' -m -r /path/do/dir | while read LINE; do php /home/scripts/watcher.php $LINE; done");

This only outputs

Array (
       [0] => /home/scripts/watcher.php 
)

If I run ionotifywait command in cli manually, it prints the expected output when a new file is uploaded

Array
(
    [0] => /home/scripts/watcher.php
    [1] => /path/to/dir/hello.jpg
)
Это было полезно?

Решение

So, here's your problem. Your call is:

passthru("inotifywait -e close_write --format '%w%f' -m -r /path/do/dir | while read LINE; do php /home/scripts/watcher.php $LINE; done");

Those double-quotes are interpolating $LINE as if it was a PHP variable. The resulting call then look like

inotifywait -e close_write --format '%w%f' -m -r /path/do/dir | while read LINE; do php /home/scripts/watcher.php ; done

... which is why the param isn't actually being passed to watcher.php.

You'll need to switch either to single quotes and judicious use of escaping for the bits that use singles in the command, or to a nowdoc if you're on 5.3 or better.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top