Ubuntu 18.04 で、ファイルがフォルダー (ディレクトリの内容の変更) (サブディレクトリではない) に置かれたかどうかを検出し、CLI 経由で PHP スクリプト (Cron/Crontab なし) を実行する方法はありますか?
答え1
前回の回答で述べたように、以下をインストールしますinotify-tools
。
sudo apt install -y inotify-tools
これで、inotifywait コマンドが使用できるようになります。
inotifywait -m /your/dir -e create -e move |
while read path action file; do
# your preferred command here
done
inotifywait --help
監視できるイベントを取得します:
Events:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted
答え2
inotify
パッケージの一部となるものを使用しますinotify-tools
。
バックグラウンドで実行されるスクリプトは次のようになります
#!/bin/sh
while :
do
inotifywatch -e moved_to -e create /watched/dir && {
php -f /path/to/script.php
}
done