# Determine if another process is locking a file

**URL:** https://discourse.julialang.org/t/determine-if-another-process-is-locking-a-file/61100
**Category:** New to Julia
**Created:** [May 13, 2021, 5:54pm UTC](https://discourse.julialang.org/t/determine-if-another-process-is-locking-a-file/61100 "2021-05-13T17:54:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [May 13, 2021, 5:54pm UTC](https://discourse.julialang.org/t/determine-if-another-process-is-locking-a-file/61100/1 "2021-05-13T17:54:09Z")

</div>

I am developing a program that will read files created by another program and process them. When I find a new file in the directory of interest, then I process that file as required.

Then I realized that it will take a while for the file to be written and my algorithm will detect the file and process it before it has been written.

I figured that the other process will have a lock on the file, and I should be able to detect that lock and wait until the lock has been released. I checked out the stat command, but it does not seem to help. Is there a method to check if another program is using the file?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 13, 2021, 5:59pm UTC](https://discourse.julialang.org/t/determine-if-another-process-is-locking-a-file/61100/2 "2021-05-13T17:59:19Z")

</div>

Perhaps you can use [Pidfile.jl](https://github.com/vtjnash/Pidfile.jl) for this purpose.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [May 13, 2021, 8:41pm UTC](https://discourse.julialang.org/t/determine-if-another-process-is-locking-a-file/61100/3 "2021-05-13T20:41:14Z")

</div>

It looks promising, but the first line in the [documentation](https://vtjnash.github.io/Pidfile.jl/) says:

> A simple utility tool for creating advisory pidfiles (lock files).

Perhaps I can only create the advisory pidfile if another process is not locking the file? Then it should work. So far I have not seen an example and have not figured out how to use it.

---

<div class="post-metadata">

### Author: ![Jake](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jake/32/46007_2.png) [@Jake](https://discourse.julialang.org/u/Jake)
#### Post date: [May 18, 2021, 11:32am UTC](https://discourse.julialang.org/t/determine-if-another-process-is-locking-a-file/61100/4 "2021-05-18T11:32:09Z")

</div>

In my case I am reading the file with some code that does not check for locks. A simple solution is to use a try catch block, and include a move statement (mv) before processing the file. The mv will error and the try catch block will catch the error.
