2

What's an easy way to detect a file has changed and update it automatically?

For example if I have a js/css file that I just uploaded, I'd like the server to detect I uploaded new js/css files and minify them automatically right then and there.

EDIT: I've tried minifying at run time and found out it's not efficient. It's interesting to note, that the file was minified for anyone requesting the file which was an overhead in itself and it was actually faster to not minify the file for delivery.

Ideally, the file should be minified within a few seconds of upload. Instead of a constantly polling system, is there an event based system out there that I could look into?

EDIT: I used mikhailov answer and added the following to the incron file:

/var/www/laravel/public/js/main.js IN_MODIFY yui-compressor -o /var/www/laravel/public/js/main.min.js /var/www/laravel/public/js/main.js 
Howard
  • 3,648
  • 13
  • 58
  • 86
  • Just give me some names so I can start. I know nothing about this. I don't even know what to google. – Howard Jul 10 '15 at 19:19
  • 2
    you could keep a table containing each file you want included, and their last modified date. then have a cron job running daily that checks your files against the recorded dates, if any are newer, then run the minify process and update the table with the new dates. It wouldn't be immediate, but it would be "automated" – RightClick Jul 10 '15 at 19:20
  • It needs to be immediate though. At least within a few seconds of upload. Is using a cron job the way to go? – Howard Jul 10 '15 at 19:21
  • you could schedule a cron job to run as frequently as once a minute – RightClick Jul 10 '15 at 19:22
  • You might want to check this post http://stackoverflow.com/questions/4030028/auto-minify-javascript-css-files-after-update – Maverick976 Jul 10 '15 at 19:22
  • @Maverick976: Thanks for the link. I understand the minifying process itself pretty well but I'd like to automate the process. – Howard Jul 10 '15 at 19:24
  • great info in that link. I'm using git so the post-commit hooks will work great for me, thanks! – RightClick Jul 10 '15 at 19:24
  • Isn't that at run time though? I already had setup something which minified at run time and found out it's not efficient. – Howard Jul 10 '15 at 19:25
  • no, the post-commit hooks would be run right after the version control system deploys the code to the server, after you commit a change to your js file. There are probably many ways of setting it up. – RightClick Jul 10 '15 at 19:35
  • @RightClick: Do you mean the link that Maverick posted? – Howard Jul 10 '15 at 19:38
  • 2
    **inotify** is what you need – Anatoly Jul 10 '15 at 19:41
  • @mikhailov: Oooh, that looks pretty good. I'd like to see what other suggestions people come up with though. – Howard Jul 10 '15 at 19:42
  • What are you using to upload the file? Maybe your file uploader has a plugin which will minify JS and CSS upon uploading. – MonkeyZeus Jul 10 '15 at 20:22
  • @MonkeyZeus: I'm just uploading via sftp. As far as I can tell it doesn't have that functionality and doesn't support any plugins but I'm open to trying something that does. – Howard Jul 10 '15 at 20:24
  • You are doing command-line sftp uploads? – MonkeyZeus Jul 10 '15 at 20:27
  • @MonkeyZeus: No, I'm using a client. – Howard Jul 10 '15 at 20:28
  • sftp is a protocol, what client are you using? – MonkeyZeus Jul 10 '15 at 20:29
  • Thanks, not sure if this post will help but I found it: http://stackoverflow.com/questions/1359473/automagically-minify-css-and-javascript-on-upload – MonkeyZeus Jul 10 '15 at 20:32
  • @MonkeyZeus: Yeah, that's interesting. I'll check it out. Thanks. – Howard Jul 10 '15 at 20:34
  • Have you tried inotify and incrond yet? – Anatoly Jul 11 '15 at 16:40
  • @mikhailov: I'll be trying it out this week. I took a break on the weekend. When you say inotify you mean this correct? http://php.net/manual/en/book.inotify.php What's incrond? Can you provide a link? – Howard Jul 13 '15 at 06:44
  • @rotaercz I'd recommend to take a look [this blog post](http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/) about inotify, it shows the similar use case that you have – Anatoly Jul 13 '15 at 06:48
  • @mikhailov: Hey, thanks! – Howard Jul 13 '15 at 06:50

1 Answers1

1

Inotify is a recommended pattern to get notified re file system events (file created, modified or deleted), what Wikipedia says:

Inotify (inode notify) is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications.

See the similar use case: how to get notified of files being copied over rsync.

Anatoly
  • 15,298
  • 5
  • 53
  • 77
  • I feel like I'm getting close but can you provide some more details on how to use it? I logged on to my server with putty and I installed it using, 'sudo apt-get install incron'. But now I'm not sure how things work? – Howard Jul 13 '15 at 17:30
  • @rotaercz to edit the incron file type *incrontab -e* and put the following content */tmp IN_ALL_EVENTS logger "/tmp action for $# file"*. Then you can see logger make an action based on new file created of deleted. The log is available from here: /var/log/messages – Anatoly Jul 13 '15 at 17:32
  • How do you stop incron? I currently went into a loop because I made it so when the js file is modified, minify it, however the minifying itself is modifying the file so it goes on forever. – Howard Jul 13 '15 at 18:17
  • 1
    This link was really helpful in figuring things out. http://www.pablumfication.co.uk/2010/09/23/incron-file-system-event-monitoring/ – Howard Jul 13 '15 at 18:57