How to detect that ftp upload is finished on a Linux server?

450 Views Asked by At

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
1

There are 1 best solutions below

2
On

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!