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

# Bash 的 prlimit 命令

prlimit [OPTION]... [CMD [ARG]...]

!subtitle:功能

查看或设置进程资源限制。

!subtitle:类型

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

!subtitle:参数

  • OPTION 选项:

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

    • -o, --output list - 打印指定的列

    • -p, --pid - 指定要操作的进程 ID;如果没有这个选项则运行 CMD

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

    • --verbose - 打印详细信息

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

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

    • -c, --core[=limits] - 核心转储文件的最大大小

    • -d, --data[=limits] - 最大数据大小

    • -e, --nice[=limits] - 允许提升的最高 nice 优先级

    • -f, --fsize[=limits] - 最大文件大小

    • -i, --sigpending[=limits] - 待处理信号的最大数量

    • -l, --memlock[=limits] - 内存锁定地址空间的最大范围

    • -m, --rss[=limits] - 最大常驻集大小

    • -n, --nofile[=limits] - 最大文件打开数量

    • -q, --msgqueue[=limits] - POSIX 消息队列中的最大字节数

    • -r, --rtprio[=limits] - 最高实时优先级

    • -s, --stack[=limits] - 栈的最大尺寸

    • -t, --cpu[=limits] - CPU 时间(秒)

    • -u, --nproc[=limits] - 最大进程数

    • -v, --as[=limits] - 地址空间限制

    • -x, --locks[=limits] - 可持有文件锁的最大数量

    • -y, --rttime[=limits] - 实时任务超时时间

  • CMD - 要执行的命令;

  • ARG - 命令的参数列表

# 列名

列名 说明
DESCRIPTION 资源描述
RESOURCE 资源名称
SOFT 软限制
HARD 硬限制
UNITS 单位

# 示例

!subtitle:查看限制

$ prlimit       # 查看当前 SHELL 的限制,等价于 prlimit -i $$
RESOURCE   DESCRIPTION                              SOFT       HARD UNITS
AS         address space limit                 unlimited  unlimited bytes
CORE       max core file size                          0  unlimited bytes
CPU        CPU time                            unlimited  unlimited seconds
DATA       max data size                       unlimited  unlimited bytes
FSIZE      max file size                       unlimited  unlimited bytes
LOCKS      max number of file locks held       unlimited  unlimited locks
MEMLOCK    max locked-in-memory address space 1823780864 1823780864 bytes
MSGQUEUE   max bytes in POSIX mqueues             819200     819200 bytes
NICE       max nice prio allowed to raise              0          0
NOFILE     max number of open files                 1024    1048576 files
NPROC      max number of processes                 55346      55346 processes
RSS        max resident set size               unlimited  unlimited bytes
RTPRIO     max real-time priority                      0          0
RTTIME     timeout for real-time tasks         unlimited  unlimited microsecs
SIGPENDING max number of pending signals           55346      55346 signals
STACK      max stack size                        8388608  unlimited bytes

!subtitle:修改限制

$ prlimit -n=10 -p $$               # 当前 SHELL 最多允许打开 10 个文件
$ prlimit -n -p $$                  # 查看当前 SHELL 最多允许打开的文件数量
RESOURCE DESCRIPTION              SOFT HARD UNITS
NOFILE   max number of open files   10   10 files
  • $$ 是当前 SHELL 的 PID

!subtitle:在限制下运行程序

prlimit --v=500M -- ./my_program    # 运行 my_program 时地址空间限制为 500M,即最多只能使用 500M 内存

# 推荐阅读

# 手册

PRLIMIT(1)                       User Commands                      PRLIMIT(1)

NAME
       prlimit - get and set process resource limits

SYNOPSIS
       prlimit [options] [--resource[=limits]] [--pid PID]

       prlimit [options] [--resource[=limits]] command [argument...]

DESCRIPTION
       Given a process ID and one or more resources, prlimit tries to retrieve
       and/or modify the limits.

       When command is given, prlimit will run this command with the given
       arguments.

       The limits parameter is composed of a soft and a hard value, separated
       by a colon (:), in order to modify the existing values. If no limits
       are given, prlimit will display the current values. If one of the
       values is not given, then the existing one will be used. To specify the
       unlimited or infinity limit (RLIM_INFINITY), the -1 or 'unlimited'
       string can be passed.

       Because of the nature of limits, the soft limit must be lower or equal
       to the high limit (also called the ceiling). To see all available
       resource limits, refer to the RESOURCE OPTIONS section.

       •   soft:_hard_ Specify both limits.

       •   soft: Specify only the soft limit.

       •   :hard Specify only the hard limit.

       •   value Specify both limits to the same value.

GENERAL OPTIONS
       --noheadings
           Do not print a header line.

       -o, --output list
           Define the output columns to use. If no output arrangement is
           specified, then a default set is used. Use --help to get a list of
           all supported columns.

       -p, --pid
           Specify the process id; if none is given, the running process will
           be used.

       --raw
           Use the raw output format.

       --verbose
           Verbose mode.

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

       -V, --version
           Print version and exit.

RESOURCE OPTIONS
       -c, --core[=limits]
           Maximum size of a core file.

       -d, --data[=limits]
           Maximum data size.

       -e, --nice[=limits]
           Maximum nice priority allowed to raise.

       -f, --fsize[=limits]
           Maximum file size.

       -i, --sigpending[=limits]
           Maximum number of pending signals.

       -l, --memlock[=limits]
           Maximum locked-in-memory address space.

       -m, --rss[=limits]
           Maximum Resident Set Size (RSS).

       -n, --nofile[=limits]
           Maximum number of open files.

       -q, --msgqueue[=limits]
           Maximum number of bytes in POSIX message queues.

       -r, --rtprio[=limits]
           Maximum real-time priority.

       -s, --stack[=limits]
           Maximum size of the stack.

       -t, --cpu[=limits]
           CPU time, in seconds.

       -u, --nproc[=limits]
           Maximum number of processes.

       -v, --as[=limits]
           Address space limit.

       -x, --locks[=limits]
           Maximum number of file locks held.

       -y, --rttime[=limits]
           Timeout for real-time tasks.

NOTES
       The prlimit(2) system call is supported since Linux 2.6.36, older
       kernels will break this program.

EXAMPLES
       prlimit --pid 13134
           Display limit values for all current resources.

       prlimit --pid 13134 --rss --nofile=1024:4095
           Display the limits of the RSS, and set the soft and hard limits for
           the number of open files to 1024 and 4095, respectively.

       prlimit --pid 13134 --nproc=512:
           Modify only the soft limit for the number of processes.

       prlimit --pid $$ --nproc=unlimited
           Set for the current process both the soft and ceiling values for
           the number of processes to unlimited.

       prlimit --cpu=10 sort -u hugefile
           Set both the soft and hard CPU time limit to ten seconds and run
           sort(1).

AUTHORS
       Davidlohr Bueso <[email protected]> - In memory of Dennis M. Ritchie.

SEE ALSO
       ulimit(1p), prlimit(2)

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

AVAILABILITY
       The prlimit 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                        PRLIMIT(1)
本文 更新于: 2026-03-06 09:52:34 创建于: 2026-03-06 09:52:34