Listing file system lock files on Linux hosts

I mentioned in a previous post that I was using the Linux flock utility to ensure that only one copy of yum would run at any given point in time (well, theoretically someone could call yum from outside of the script, but there are only so many use cases you can protect against). The lock files that are created by flock reside on a file system, and can be viewed with the lslk utility:

$ lslk

SRC        PID DEV     INUM SZ TY M ST WH END LEN NAME
(unknown) 1536 8,1   927544     w 0  0  0   0   0 / (rootfs)
atd       1785 8,1   927573  5  w 0  0  0   0   0 /var/run/atd.pid
(unknown) 2034 8,1 14963655     w 0  0  0   0   0 / (rootfs)



If the file name doesn’t appear in the lslk output, you can use the find utilities “-inum” option (find a file by its inode number) to locate the file using the inode number listed in the 4th column:

$ find . -inum 14963655
./lock

If the process name doesn’t show up, you can use the lsof utility along with the name of the lock to see which process has the lock file open:

$ lsof | awk ‘$9 ~ /lock/ { print }’

test      2033      root  200w      REG                8,1        0   14963655 /tmp/lock
sleep     2035      root  200w      REG                8,1        0   14963655 /tmp/lock



I have been using lslk off and on for years, and it’s a SUPER useful tool for debugging issues with file system lock files. Nice!

Leave a Comment