0

I am using fswatch to monitor a directory and run a script when video files are copied into that directory:

fswatch -o /Path/To/Directory/Directory | xargs -n 1 sh /Path/To/Script/Script.sh

The problem is that the file is often not completed its copy before the script is actioned. The files are video files of varying size. Small files are OK, larger files are not.

How can I delay the fswatch notification until the file has completed its copy?

ssmc
  • 21
  • 6

1 Answers1

2

First of all, the behaviour of the fswatch "monitors" is OS-specific: when asking question about fswatch you'd better specify the OS you use.

However, there's no way to do that using fswatch alone. A process may open a file for writing and keep it open for an amount of time sufficiently long for the OS to send multiple events. I'm afraid there is nothing fswatch can do about it.

An alternate approach may be using another tool to check whether the modified file is currently open: if it is not, then run your script, otherwise skip it and wait for its next event. Such tools are OS-specific: in OS X and Linux you may use lsof. Beware this approach does not protect you from another process opening that file while your script is running.

Enrico M. Crisostomo
  • 1,653
  • 1
  • 17
  • 26