Home » Php » php – How to efficiently monitor a directory for changes on linux?

php – How to efficiently monitor a directory for changes on linux?

Posted by: admin July 12, 2020 Leave a comment

Questions:

I am working with Magento, and there is a function that merges CSS and Javascript into one big file.

Regardless the pros and cons of that, there is the following problem:

The final file gets cached at multiple levels that include but are not limited to:

  • Amazon CloudFront
  • Proxy servers
  • Clients browser cache

Magento uses an MD5 sum of the concatenated css filenames to generate a new filename for the merged css file. So that every page that has a distinct set of css files gets a proper merged css file.

To work around the caching issue, I also included the file modification timestamps into that hash, so that a new hash is generated, everytime a css file is modified.

So the full advantages of non revalidative caching score, but if something gets changed, its visible instantly, because the resource link has changed.

So far so good:

Only problem is, that the filenames that are used to generate the has, are only the ones that would normally be directly referenced in the HTML-Head block, and don’t include css imports inside those files.

So changes in files that are imported inside css files don’t result in a new hash.

No I really don’t want to recursively parse all out imports and scan them or something like that.

I rather thought about a directory based solution. Is there anything to efficiently monitor the “last change inside a directory” on a file system basis?

We are using ext4.

Or maybe is there another way, maybe with the find command, that does all the job based on inode indexes?

Something like that?

I have seen a lot of programs that instantly “see” changes without scanning whole filesystems. I believe there are also sort of “file manipulation watch” daemons available under linux.

The problem is that the css directory is pretty huge.

Can anyone point me in the right direction?

How to&Answers:

I suggest you use php-independent daemon to modify change date of your main css file when one of dependent php files are modified. You can use dnotify for it, something like:

dnotify -a -r -b -s /path/to/imported/css/files/ -e touch /path/to/main/css/file;

It will execute ‘touch’ on main css file each time one of the files in other folder are modified (-a -r -b -s = any access/recursive directory lookup/run in background/no output). Or you can do any other action and test for it from PHP.

Answer:

If you use the command

ls -ltr `find . -type f `

It will give you a long listing of all files with the newest at the bottom.

Answer:

Try to have a look to inotify packages that will allows you to be notified eah time a modification occurs in a directory.

Answer:

I’ve never used it, but apparently there is inotify support for PHP.

(inotify would be the most efficient way to get notifications under Linux)