Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to determine what process has ownership of a lock-file. The lock-files are simply a file with a specific name that has been created.

So, how can I determine what process has a particular file open in Linux? Preferably a one-liner type or a particular Linux tool solution would be optimal.

share|improve this question
up vote 27 down vote accepted

You can also use fuser for this:

~> less .vimrc
# put in background
~> fuser .vimrc
.vimrc:              28135
~> ps 28135
28135 pts/36   T      0:00 less .vimrc
share|improve this answer
this was great, but to use it in a script i had to check the output length. – chovy Aug 21 '13 at 7:39
what do you mean the output length? – Nathan Fellman Aug 21 '13 at 8:40
if [ fuser "$file"` ]; then exit` – chovy Aug 21 '13 at 9:05
fuser has strange behavior with exit codes. it returns 1 exitcode with two states: A/ some internal error, checked file not found etc, B/ no process opened specified file. In situation A/ some error message is printed to its output. Unfortunately when file is available and opened by something, output is generated but with exit code 0. It would be better if fuser will exit with three codes, not two like currently. lsoft is a bit worse resolve because this is working more slowly. – Znik Dec 11 '14 at 10:01

On most Linux systems lsof NAME does the job:

fin@r2d2:~$ lsof /home/fin
bash    21310  fin  cwd    DIR    8,1 4096 5054467 /home/fin
lsof    21320  fin  cwd    DIR    8,1 4096 5054467 /home/fin
lsof    21321  fin  cwd    DIR    8,1 4096 5054467 /home/fin
share|improve this answer
And what if you don't have lsof? – JoseLSegura Jan 9 '14 at 11:54
@JoseLSegura: I assuming you're resourceful enough for the answer 'then install lsof' to be useless for you. Can you elaborate on your problem? If you don't have root, you likely don't have privs to find out if another user has the file open anyhow. – Michael Scheper Dec 10 '15 at 0:48

Having a file open is not a lock because, if each process has to check whether the file is open first and not proceed if it is or create/open it if it isn't, then two processes could quite well check simultaneously, both find that it isn't open, then both create or open it.

To use a file as a lock, the check-and-lock operation has to be a single uninterruptable operation. You can achieve this in a Unix filesystem by creating a file with read-only mode and removing it to unlock. If the file exists (and is read only) the file creation will fail, so you get check-and-lock in a single atomic operation.

If your locking process is a shell script that will be running as a daemon, you can get this effect by using umask, a per-process setting that sets the permissions that new files are created with:

umask 222   # create files unwritable to owner too
if echo $$ > /var/lock/foo
    : locking succeeded
    : locking failed
umask $oldumask
This also writes the owning process' PID into the file, which solves your other problem: cat /var/lock/foo
As regards the specific question "Which processes have this file open?", this can be useful when you want to unmount a filesystem but can't because some process has a file open in it. If you don't have those commands available, you can ask /proc as root:

ls -l /proc/*/cwd | grep '/var/lock/foo$'

or, as a mortal user:

ls -l /proc/*/cwd 2>/dev/null | grep '/var/lock/foo$'

share|improve this answer
the `ls -l' method works for Linux but seems not to be working for CygWin: there is no info about file locking there. Wouldn't you know how to solve? Thanks. – Sopalajo de Arrierez Apr 9 '14 at 21:23

I found that using the accepted answer didn't list the processes that were using my directory ( ubuntu 14.04 ).

In the end, I used lsof (list open files) and grepped its output to find the offending process:

lsof | egrep "<regexp-for-your-file>"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .