0

I would like to run a php script on a folder whenever content in that folder changes (a file is edited, or overwritten, or added or deleted).

The php script gets a hash code for each file in the folder, and writes all the lines in a hash.txt file (appending a line like hashcode:filename for each file, e.g. 2dbb8badb0925833c8ea03ffc941e3a0:file1_blabla.utf8).

After adding my user to /etc/incron.allow, I have edited my icrontab -e in the server, and added the following line:

/var/www/mydir/tests/ IN_ALL_EVENTS php -f /var/www/mydir/tests/mk_hash_list.php

The result: some kind of killer loop that collapse my server with an ever growing log, which shows lines like:

Jun 24 19:24:30 myserver incrond[22010]: (manuel) CMD (php -f /var/www/mydir/tests/hash_list.php)
Jun 24 19:24:30 myserver incrond[22010]: cannot fork process: Resource temporarily unavailable

Any tips? Is icrontab good for what I'm trying to do? Thanks.

msoutopico
  • 357
  • 3
  • 15
  • 1
    I have no experience with incron, but my common sence tells me `IN_ALL_EVENTS` triggers a script in the watched folder that writes to a file in the watched folder. Looks like a never ending loop. – Michel Jun 25 '20 at 09:36
  • Yes, exactly. Duh! Since any event to any file in the folder triggers an update of one file in that folder, it creates an infinite loop. It seems so obvious now! Thank you, if you add it as an answer I'll mark it as the right answer. – msoutopico Jul 01 '20 at 14:55
  • Possible solutions I'm looking into: 1) watch only files with a certain extension, 2) add a watch exception (an exclusion pattern that matches the file that should not be watched), 3) write he hash file elsewhere. – msoutopico Jul 01 '20 at 14:55
  • 1
    Isn't it much easier to just move the executed script and the log file outside the watched folder? – Michel Jul 02 '20 at 05:30
  • Indeed, that's what I did, and it works fine. That's what I meant in my third option. Thanks @Michel – msoutopico Jul 11 '20 at 20:22

1 Answers1

0

The file to which the script appended data was written in the watched folder, creating an endless loop. The solution was to create a subfolder containing only the files that the script should analyze and keep both the script and the written file with the result outside of that watched subfolder.

msoutopico
  • 357
  • 3
  • 15