nice 修改程序的优先级

发布时间: 更新时间: 总字数:454 阅读时间:1m 作者: IP上海 分享 网址

Linux可以使用nice命令修改程序的优先级

介绍

  • Linux 运行 COMMAND 通过 优先级(niceness) 影响进程调度
  • 优先级 值范围从 -20(对进程最有利)到 19(对进程最不利)
    • 优先级的值越低,优先级越高
    • 优先级的值越高,优先级越低

安装

apt install coreutils

$ dpkg -S `which nice`
coreutils: /usr/bin/nice

help

nice --help ...
$ nice --help
Usage: nice [OPTION] [COMMAND [ARG]...]
Run COMMAND with an adjusted niceness, which affects process scheduling.
With no COMMAND, print the current niceness.  Niceness values range from
-20 (most favorable to the process) to 19 (least favorable to the process).

Mandatory arguments to long options are mandatory for short options too.
  -n, --adjustment=N   add integer N to the niceness (default 10)
      --help        display this help and exit
      --version     output version information and exit

NOTE: your shell may have its own version of nice, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

Exit status:
  125  if the nice command itself fails
  126  if COMMAND is found but cannot be invoked
  127  if COMMAND cannot be found
  -    the exit status of COMMAND otherwise

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/nice>
or available locally via: info '(coreutils) nice invocation'

使用

程序运行前修改

# 以最高优先级运行
$ nice -n-20 bash xxx.sh

# 以最低优先级运行
$ nice -n19 bash xxx.sh

修改运行中的程序

  • renice 修改
renice -20 PID
renice -19 PID
  • top 修改
$ top  # 输入 r 后提示 `PID to renice [default pid = 2161]` 输入pid或回车后,提示 `Renice PID 2161 to value`,输入需要的优先级回车即可

查看优先级

  • ps -lef 中的 NI
$ ps -lef | more
F S UID          PID    PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root           1       0  0  80   0 -  5535 ep_pol 14:08 ?        00:00:02 /sbin/init
  • top 中的 NI
    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
      1 root      20   0   22140  13384   9544 S   0.0   0.3   0:02.39 systemd
Home Archives Categories Tags Statistics
本文总阅读量 次 本站总访问量 次 本站总访客数