Is there a way to check if a file is currently open somewhere?

I have a big simulation file being in process of written, and I need to know when it stops being written.

Kind regards

lsof on Linux. For other platforms, search the web for examples of emulating lsof.

It seems like think the built in file watching API should be able to do this, but I wasn’t able to come up with an example that worked.

Here is a solution using inotify (you may need to apt install inotify-tools or the equivalent for your distro).
In one terminal, start a process writing to the file:

$ (while true; do echo hello; sleep 1; done) > /tmp/writing.txt

Then, in a julia repl:

julia> run(`inotifywait -e close_write /tmp/writing.txt`)
Setting up watches.
Watches established.

Switch back to the first terminal and interrupt the writing process with Ctrl-C, you should see this in the julia repl:

/tmp/writing.txt CLOSE_WRITE,CLOSE
Process(`inotifywait -e close_write /tmp/writing.txt`, ProcessExited(0))

I couldn’t get it to work either, but as you say FileWatching should be able to do it, see the help:

help?> poll_fd
search: poll_fd poll_file

  poll_fd(fd, timeout_s::Real=-1; readable=false, writable=false)

  Monitor a file descriptor fd for changes in the read or write availability, and with a timeout given by timeout_s

  The keyword arguments determine which of read and/or write status should be monitored; at least one of them must be
  set to true.

  The returned value is an object with boolean fields readable, writable, and timedout, giving the result of the

" Monitor a file descriptor fd for changes in the read or write availability, … "

I unfortunately need the solution for windows, yours is Linux as far as I can see :confused:

Kind regards

Why not just write some “computation finished” token to the end of the file when the simulation is done?

I often write another file that summarizes the simulation results (also status, like finishing time) when the simulation is done.

1 Like

It is because I do not control the simulation output, I only start the simulation. It is some other software I am using. Since it generates big data files, I wish to post-process the simulation as it runs. Therefore I need to be able to detect when a simulation file has been generated so I can start processing it.

Kind regards