Wayne Stallwood ALUGlist@digimatic.plus.com writes:
What is the most efficient way to "watch" a folder for a particular file pattern, if I have a looping "if exist" script is this likely to consume lots of resources as surely it would be continuously polling the disk to check for that file.
Every time a file is created in a directory the directory's modification time will be updated. So the answer is to poll that, and only do a full scan when it changes.
So the logic would be:
latest_time = mtime of directory scan for pattern while true: new_time = mtime of directory if current_time != new_time: sleep 1 second scan for pattern latest_time = new_time else: sleep 1 second (or 10s or 1m or whatever)
The 1 second sleep is necessary for fully reliable operation.
It will hit the physical disk less than you imagine, as usually the directory's inode will be cached.
This would happen in a single long-running process, rather than from cron.