brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: lslocks shows which process holds a lock on which file

Linux
~/tips/tip-linux-lslocks-who-holds-lock

lslocks

Lists file locks held on the system with the process that owns each one.

“Resource temporarily unavailable” or a program that waits forever often means a file lock. Finding the holder used to mean guessing.

The man page says lslocks lists information about all the currently held file locks on a Linux system:

lslocks
lslocks -p 1234
lslocks -o COMMAND,PID,PATH

The default output includes the command and PID, the type and mode of the lock, the size and the path of the locked file. The -p option shows only the locks held by one process, and -o picks the columns. The available names include COMMAND, PID, TYPE, SIZE, INODE, MAJ:MIN, MODE, M, START, END, PATH, BLOCKER and HOLDERS, and a leading plus adds columns to the default set:

lslocks -o +BLOCKER

The BLOCKER column shows the process that holds up a lock you are waiting for, which is the answer you were looking for in the first place. For scripts, -n leaves out the header and -r gives raw output.

The TYPE column covers FLOCK, POSIX, OFD and LEASE locks. These are kernel locks. A program that keeps its own lock file, a plain file whose existence means busy, will not show up here, because no kernel lock is involved. In that case look at the file with ls, and use lsof or fuser to see which process has it open.

A lock is also tied to an open file description, which several processes can share, so lslocks shows only one holder in COMMAND and PID. For some lock types the PID can even show as -1.

Further reading: lslocks(8) manual page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link