国际访客建议访问 Primers 编程伙伴 国际版站点 > Bash 教程 > lslocks 以获得更好的体验。

# Bash 的 lslocks 命令

lslocks [OPTION]...

!subtitle:功能

列出系统中的锁。

!subtitle:类型

可执行文件(/usr/bin/lslocks),属于 util-linux

!subtitle:参数

  • OPTION 选项:

    • -b, --bytes - 使用字节作为单位

    • -i, --noinaccessible - 忽略无法访问的锁

    • -J, --json - 以 JSON 格式输出

    • -n, --noheadings - 不显示标题行(列名)

    • -o, --output list - 指定要打印的列,参考 列名)

    • --output-all - 打印所有可用的列

    • -p, --pid pid - 只打印 PID 指定进程持有的锁

    • -r, --raw - 以原始格式输出

    • -u, --notruncate - 不截断列中的文本

    • -h, --help - 显示帮助

    • -V, --version - 显示版本

# 列名

列名 说明
COMMAND 持有锁的进程命令
PID 持有锁的进程 PID
TYPE 锁的类型
SIZE 锁的大小
INODE inode 节点号
MAJ:MIN 主要设备号:次要设备号
MODE 锁的访问模式
M 是(1)否(0)为 强制性锁
START 锁的相对字节偏移量
END 锁末端的偏移量
PATH 锁文件的路径
BLOCKER 被锁阻塞进程的 PID

# 示例

$ $ lslocks
COMMAND             PID  TYPE SIZE MODE  M START        END PATH
mysqld           758338 POSIX      WRITE 0     0          0 /...
mysqld           758338 POSIX      WRITE 0     0          0 /...
mysqld           758338 POSIX      WRITE 0     0          0 /...
mysqld          2844183 POSIX      WRITE 0     0          0 /...
mysqld          2844183 POSIX      WRITE 0     0          0 /...
mysqld          2844183 POSIX      WRITE 0     0          0 /...
mysqld          2844183 POSIX      WRITE 0     0          0 /...
mysqld          2844183 POSIX      WRITE 0     0          0 /...
dockerd            1054 FLOCK      WRITE 0     0          0 /...
dockerd            1054 FLOCK      WRITE 0     0          0 /...
dockerd            1054 FLOCK      WRITE 0     0          0 /...
cron            2844018 FLOCK      WRITE 0     0          0 /run...
...

# 推荐阅读

# 手册

LSLOCKS(8)                   System Administration                  LSLOCKS(8)

NAME
       lslocks - list local system locks

SYNOPSIS
       lslocks [options]

DESCRIPTION
       lslocks lists information about all the currently held file locks in a
       Linux system.

       Note that lslocks also lists OFD (Open File Description) locks, these
       locks are not associated with any process (PID is -1). OFD locks are
       associated with the open file description on which they are acquired.
       This lock type is available since Linux 3.15, see fcntl(2) for more
       details.

OPTIONS
       -b, --bytes
           Print the sizes in bytes rather than in a human-readable format.

           By default, the unit, sizes are expressed in, is byte, and unit
           prefixes are in power of 2^10 (1024). Abbreviations of symbols are
           exhibited truncated in order to reach a better readability, by
           exhibiting alone the first letter of them; examples: "1 KiB" and "1
           MiB" are respectively exhibited as "1 K" and "1 M", then omitting
           on purpose the mention "iB", which is part of these abbreviations.

       -i, --noinaccessible
           Ignore lock files which are inaccessible for the current user.

       -J, --json
           Use JSON output format.

       -n, --noheadings
           Do not print a header line.

       -o, --output list
           Specify which output columns to print. Use --help to get a list of
           all supported columns.

           The default list of columns may be extended if list is specified in
           the format +list (e.g., lslocks -o +BLOCKER).

       --output-all
           Output all available columns.

       -p, --pid pid
           Display only the locks held by the process with this pid.

       -r, --raw
           Use the raw output format.

       -u, --notruncate
           Do not truncate text in columns.

       -h, --help
           Display help text and exit.

       -V, --version
           Print version and exit.

OUTPUT
       COMMAND
           The command name of the process holding the lock.

       PID
           The process ID of the process which holds the lock or -1 for
           OFDLCK.

       TYPE
           The type of lock; can be FLOCK (created with flock(2)), POSIX
           (created with fcntl(2) and lockf(3)) or OFDLCK (created with
           fcntl(2)).

       SIZE
           Size of the locked file.

       MODE
           The lock’s access permissions (read, write). If the process is
           blocked and waiting for the lock, then the mode is postfixed with
           an '*' (asterisk).

       M
           Whether the lock is mandatory; 0 means no (meaning the lock is only
           advisory), 1 means yes. (See fcntl(2).)

       START
           Relative byte offset of the lock.

       END
           Ending offset of the lock.

       PATH
           Full path of the lock. If none is found, or there are no
           permissions to read the path, it will fall back to the device’s
           mountpoint and "..." is appended to the path. The path might be
           truncated; use --notruncate to get the full path.

       BLOCKER
           The PID of the process which blocks the lock.

NOTES
       The lslocks command is meant to replace the lslk(8) command, originally
       written by Victor A. Abell <[email protected]> and unmaintained since
       2001.

AUTHORS
       Davidlohr Bueso <[email protected]>

SEE ALSO
       flock(1), fcntl(2), lockf(3)

REPORTING BUGS
       For bug reports, use the issue tracker at
       https://github.com/util-linux/util-linux/issues.

AVAILABILITY
       The lslocks command is part of the util-linux package which can be
       downloaded from Linux Kernel Archive
       <https://www.kernel.org/pub/linux/utils/util-linux/>.

util-linux 2.39.3                 2023-12-01                        LSLOCKS(8)
本文 更新于: 2026-03-06 09:52:32 创建于: 2026-03-06 09:52:32