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

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692
**Category:** New to Julia
**Created:** [September 2, 2022, 2:26pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692 "2022-09-02T14:26:47Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 2, 2022, 2:26pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/1 "2022-09-02T14:26:47Z")

</div>

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

Kind regards

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [September 2, 2022, 2:53pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/2 "2022-09-02T14:53:24Z")

</div>

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

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [September 2, 2022, 9:12pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/3 "2022-09-02T21:12:19Z")

</div>

It seems like think the built in file watching [API](https://docs.julialang.org/en/v1/stdlib/FileWatching/) 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:

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

```

Then, in a julia repl:

```julia
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:

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

```

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 2, 2022, 9:21pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/4 "2022-09-02T21:21:22Z")

</div>

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

```julia
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
  seconds.

  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
  polling.

```

" 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 😕

Kind regards

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 3, 2022, 1:15am UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/5 "2022-09-03T01:15:26Z")

</div>

> [@Ahmed\_Salih](#):
>
> I have a big simulation file being in process of written, and I need to know when it stops being written.

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

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [September 3, 2022, 1:24am UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/6 "2022-09-03T01:24:59Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)
#### Post date: [September 3, 2022, 6:58am UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-if-a-file-is-currently-open-somewhere/86692/7 "2022-09-03T06:58:24Z")

</div>

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
