0

I have an inotifywait script that monitors a ftp upload directory and whenever a new file is uploaded it sets the correct permissions.

I do this because the remote client forces wrong file modes and preventing the ftp server to accept chmod commands causes the remote client to spit out bogus errors.

so my last resort is an inotify script but that causes yet another problem, here it is:

#!/bin/bash
inotifywait -mrq -e ATTRIB --format "%w%f" /home/user/upload/ | while read FILE; do 
  if [[ -f "$FILE" ]];then
    chown user:apache "$FILE" && chmod 640 "$FILE"
  fi
done

so my guess what goes wrong here is that once the file is uploaded and chmodded by the client the ATTRIB option gets triggered (as it is supposed to) but the resulting chmod in my own script causes the ATTRIB option to be triggered again... causing a recursive loop.

is there a way to make clear to inotifywait that it should ignore its own actions preventing this loop?

chichi
  • 3
  • 2

1 Answers1

0

Instead of having inotifywait wait for -e attrib, you should be waiting for -e modify.

   modify
   A watched file or a file within a watched directory was written to.

   attrib
   The metadata of a watched file or a file within a watched directory was modified.  This includes timestamps,  file permissions, extended attributes etc.
Eduardo Trápani
  • 1,210
  • 8
  • 12
  • well.. that works only for the newest file, all older (previously uploaded) files will get chmodded back again to the wrong modes by the ftp client everytime it syncs and it wont be noticed by MODIFY. i'm starting to believe this is some sort of unsolvable catch 22. – chichi Jan 22 '20 at 21:21
  • replacing it with: find $TARGETDIR -type f -exec chmod 0640 {} + -o -type d -exec chmod 0755 {} + almost solves it but it seems like it doesnt chmod all files, everytime it ran, randomly some files are still have wrong modes. probably because now both inotify and the ftp-client are both at the same time chmodding the files.... :( i tried adding a 5 second delay but that still doesnt solve it and it means that for at least 5 seconds all files have wrong modes. – chichi Jan 23 '20 at 08:42