Linux shell编程学习笔记65: nice命令 显示和调整进程优先级

0 前言

我们前面学习了Linux命令ps和top,命令的返回信息中包括优先序(NI,nice) ,我们可以使用nice命令来设置进程优先级。

1 nice命令 的功能、格式和选项说明

1.1 nice命令 的功能

nice命令的功能是用于调整进程的优先级,帮助系统资源的合理分配。

Linux系统后台运行着不少进程,其中有一些进程,例如定期数据备份、自动清理垃圾等等工作,我们都可以通过nice命令调低其执行优先级,从而把硬件等资源留给重要的工作。

优先级范围为-20~19,数字越小,优先级越高,默认为10。

我们可以使用命令nice --help 来查看帮助信息。 

[prupleEndurer @ bash ~ ] 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.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report nice translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'nice invocation'
[prupleEndurer @ bash ~ ] 

1.2 nice命令 的格式

nice [选项] [命令 [参数]...]

1.3 nice命令 的选项说明

选项说明
 -g PGID
 

匹配进程组ID

有些nice版本不支持本选项

-n N

--adjustment=N

将优先级调整为N,-20≤ N ≤19,默认为10
-p PID

匹配进程ID(PID)

有些nice版本不支持本选项

-u UID

匹配用户ID(UID)

有些nice版本不支持本选项

--help显示此帮助并退出
--version输出版本信息并退出

 2 nice 命令实例 

2.1 nice:显示nice命令执行历史

[prupleEndurer @ bash ~ ] nice
0
[prupleEndurer @ bash ~ ] nice top &
[1] 210
[prupleEndurer @ bash ~ ] nice
0

[1]+  Stopped                 nice top
[prupleEndurer @ bash ~ ] ps -l
F S   UID     PID    PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S  1000     118       0  0  80   0 - 10039 -      pts/3    00:00:00 zsh
4 S  1000     195     118  0  80   0 -  3781 -      pts/3    00:00:00 bash
4 T  1000     210     195  0  90  10 - 14239 -      pts/3    00:00:00 top
0 R  1000     212     195  0  80   0 - 13232 -      pts/3    00:00:00 ps
[prupleEndurer @ bash ~ ] nice
0
[prupleEndurer @ bash ~ ] 

我们第一次执行命令nice,返回信息为0

接着我们执行命令 nice top &,以后台方式、优先级默认为10运行top,返回信息中的210是top命令对应的进程ID(PID)

第二次执行命令nice,返回信息比第一次执行时多了一行内容:

[1]+  Stopped                 nice top

也就是刚才运行的命令。

接下来我们执行命令 ps -l,从命令返回信息中可以看到top命令对应的进程ID(PID)210的NI值为默认值10。

2.2 nice -n 命令:以指定优先级执行命令

[prupleEndurer @ bash ~ ] nice -n 5 top

Tasks:  13 total,   1 running,  10 sleeping,   2 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.2 id,  0.0 wa,  0.2 hi,  0.0 si,  0.0 st
KiB Mem :  3855940 total,  2005076 free,   275256 used,  1575608 buff/cache
KiB Swap:        0 total,        0 free,        0 used.  3332552 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                                        
      1 csdn      20   0   40356   5152   3780 S   0.0  0.1   0:00.23 zsh                                                                                            
     47 csdn      20   0   40156   5104   3844 S   0.0  0.1   0:00.04 zsh                                                                                            
     73 csdn      20   0   40156   4924   3684 S   0.0  0.1   0:00.04 zsh                                                                                            
    102 csdn      20   0   15124   3540   3124 S   0.0  0.1   0:00.00 bash                                                                                           
    116 csdn      30  10   56956   3912   3508 T   0.0  0.1   0:00.00 top                                                                                            
    118 csdn      20   0   40156   5044   3780 S   0.0  0.1   0:00.04 zsh                                                                                            
    147 csdn      20   0   40156   4956   3716 S   0.0  0.1   0:00.04 zsh                                                                                            
    195 csdn      20   0   15124   3464   3048 S   0.0  0.1   0:00.00 bash                                                                                           
    210 csdn      30  10   56956   3960   3556 T   0.0  0.1   0:00.00 top                                                                                            
    214 csdn      20   0   40156   4960   3716 S   0.0  0.1   0:00.04 zsh                                                                                            
    239 csdn      20   0   40156   5044   3780 S   0.0  0.1   0:00.05 zsh                                                                                            
    293 csdn      20   0   15124   3484   3132 S   0.0  0.1   0:00.00 bash                                                                                           
    306 csdn      25   5   59472   4384   3840 R   0.0  0.1   0:00.00 top                            


 

我们先执行命令nice -n 5 top,以优先级5来执行top命令

然后我们在top命令显示的信息中,可以看到top命令对应的进程的NI为5

  • 16
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 20
    评论
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值