How do I find out what process has a lock on a file in Linux?

The fuser Unix command will give you the PIDs of the processes accessing a file.


lslocks lists information about all the currently held file locks in a Linux system. (part of util-linux) this utility has support for json output, which is nice for scripts.

~$ sudo lslocks
COMMAND           PID   TYPE SIZE MODE  M START END PATH
cron              873  FLOCK   4B WRITE 0     0   0 /run/crond.pid

..
..


fuser will show you which processes are accessing a file or directory.


Use lsof to find out what has what files are open.

man lsof or have a look here