1

I'm using inotify-tools on CentOS 7 to execute a php script on every ftp upload.

It's working fine, but there is one problem; when the upload gets aborted (for example if I stop uploading or close FTP client) then it still triggers the script.

Is this possible to avoid those situations?

My code:

#!/bin/sh
MONITORDIR="/path/to/some/dir"
inotifywait -m -r -e close_write --format '%w%f' "${MONITORDIR}" | while read NEWFILE
do
    php /path/to/myscript.php ${NEWFILE}
done
HTMHell
  • 5,761
  • 5
  • 37
  • 79

1 Answers1

1

Thing is: your shell script there contains a sequence of commands.

But you do not have any checking if these commands were successful. And you are surprised that they are all executed; even when one failed?!

Depending on how those tools you are calling work, it might be enough to simply add

set -e

prior calling any commands (see here for details)

If that doesn't cut it: run your commands one by one; and determine for each one if it failed; and if so; simply exit your script!

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • I've tried to add `set -e` at the beginning, didn't help. And how do I determine if each command was failed? – HTMHell Dec 09 '16 at 09:07
  • Well, on a first glance, commands use return codes (and that is what set -e is checking). If that doesn't help ... you simple have to look into each of those commands and figure "how could I determine that it worked" – GhostCat Dec 09 '16 at 09:17