Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to efficiently monitor a directory for changes on linux?

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?

like image 330
The Surrican Avatar asked Jul 22 '11 10:07

The Surrican


People also ask

How do I monitor a folder for changes?

Locate the file or folder whose permission changes you wish to track. Right click on it and go to Properties. In the Security tab, click the Advanced button. In Advanced Security Settings for Active Directory window, go to Auditing tab, and click the Add button to add a new auditing entry.

How do I see changes in Linux?

In Linux, the default monitor is inotify. By default, fswatch will keep monitoring the file changes until you manually stop it by invoking CTRL+C keys. This command will exit just after the first set of events is received. fswatch will monitor changes in all files/folders in the specified path.


2 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.

like image 184
XzKto Avatar answered Oct 07 '22 18:10

XzKto


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.

like image 22
Scott C Wilson Avatar answered Oct 07 '22 18:10

Scott C Wilson