Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cronjob to detect last modified files

I want to run a cronjob every minute to detect all files that were changed in the last minute in a specific directory (with about 300.000 inodes) and export this file list to a csv.

Is it possible to run an optimized command to do that? I cant run a "find" with sort flag in this directory cause it is huge and it will probably take more than 1 minute to run all files.

Is there any command I can do that? Or run any specific program on the background of the server that logs every changed file as it is changed? If there is a command using PHP to do this I am fine, I can create a cron to execute a PHP script, no problem.

like image 327
Samul Avatar asked Dec 30 '15 00:12

Samul


1 Answers

There is a Linux utility called incron that can be used similar to normal cron, but rather than events being time based, they work off of inotify and are fired from file events.

You can find the Ubuntu man page here: http://manpages.ubuntu.com/manpages/intrepid/man5/incrontab.5.html

I personally have not had to use it for anything too complex, but it roughly goes like this:

Install it:

sudo apt-get install incron

Open the editor to add an entry:

incrontab -e

Put something like this:

/var/www/myfolder IN_MODIFY curl https://www.example.com/api/file-updated/$#

The first part is the file or folder to watch. The second part is the event. And the third part is the command.

I think that $# is the placeholder for the file in question.

like image 160
Jeremy Harris Avatar answered Sep 29 '22 10:09

Jeremy Harris