欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > Linux shell编程学习笔记65: nice命令 显示和调整进程优先级

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

2024/10/25 5:11:55 来源:https://blog.csdn.net/Purpleendurer/article/details/140507225  浏览:    关键词: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 exitNOTE: 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 topTasks:  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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com