Debounce inotifywait for large files

I upload files for deployment into a remote directory. That remote server has a script that watches the directory for new files:

inotifywait --monitor --event create --format '%f' --quiet /foo

When a new file is detected, the deployment process starts.

The problem is the upload takes time – and the file is detected as soon as it starts writing. So the deployment fails as it attempts to use a partial file.

Is there a way to debounce the inotifywait so it reports the new file only after it is fully created?

Asked By: lonix

||

As you have experienced, watching for create events isn’t very useful: these events trigger when the file is created, but that doesn’t tell you if any data has been written to it, nor do you know when something has finished writing data to it.

You will generally want to monitor the close or close_write events. From the man page:

EVENTS

  • close_write

    A watched file or a file within a watched directory was closed,
    after being opened in writeable mode. This does not necessarily
    imply the file was written to.

  • close_nowrite

    A watched file or a file within a watched directory was closed,
    after being opened in read-only mode.

  • close

    A watched file or a file within a watched directory was closed,
    regardless of how it was opened. Note that this is actually
    implemented simply by listening for both close_write and
    close_nowrite, hence all close events received will be output as
    one of these, not CLOSE.

Answered By: larsks
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.