该Shell命令最常用的使用方式为:nice [-n <优先等级>][执行指令],其中优先等级的范围从-20-19,其中-20最高,19最低,只有系统管理者可以设置负数的等级。```···
#后台执行sleep 100秒,同时在启动时将其nice值置为19
[root@xieqichao ~]# nice -n 19 sleep 100 &
[1] 4661
#后台执行sleep 100秒,同时在启动时将其nice值置为-19
[root@xieqichao ~]# nice -n -19 sleep 100 &
[2] 4664
#关注ps -l输出中用黄色高亮的两行,它们的NI值和我们执行是设置的值一致。
[root@xieqichao ~]# ps -l
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
4 S 0 2833 2829 0 80 0 - 1739 - pts/2 00:00:00 bash
0 S 0 4661 2833 0 99 19 - 1066 - pts/2 00:00:00 sleep
4 S 0 4664 2833 0 61 -19 - 1066 - pts/2 00:00:00 sleep
4 R 0 4665 2833 1 80 0 - 1231 - pts/2 00:00:00 ps
renice命令主要用于为已经执行的进程重新设定nice值,该命令包含以下几个常用选项:
选项 | 说明 |
---|
-g | 使用程序群组名称,修改所有隶属于该程序群组的程序的优先权。 |
-p | 改变该程序的优先权等级,此参数为预设值。 |
-u | 指定用户名称,修改所有隶属于该用户的程序的优先权。 |
#切换到stephen用户下执行一个后台进程,这里sleep进程将在后台睡眠1000秒。
[root@xieqichao ~]# su stephen
[root@xieqichao ~]# sleep 1000&
[1] 4812
[root@xieqichao ~]# exit #退回到切换前的root用户
#查看已经启动的后台sleep进程,其ni值为0,宿主用户为stephen
[root@xieqichao ~]# ps -eo user,pid,ni,command | grep stephen
stephen 4812 0 sleep 1000
root 4821 0 grep stephen
#以指定用户的方式修改该用户下所有进程的nice值
[root@xieqichao ~]# renice -n 5 -u stephen
500: old priority 0, new priority 5
#从再次执行ps的输出结果可以看出,该sleep后台进程的nice值已经调成了5
[root@xieqichao ~]# ps -eo user,pid,ni,command | grep stephen
stephen 4812 5 sleep 1000
root 4826 0 grep stephen
#以指定进程pid的方式修改该进程的nice值
[root@xieqichao ~]# renice -n 10 -p 4812
4812: old priority 5, new priority 10
#再次执行ps,该sleep后台进程的nice值已经从5变成了10
[root@xieqichao ~]# ps -eo user,pid,ni,command | grep stephen
stephen 4812 10 sleep 1000
root 4829 0 grep stephen
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
<div class="more-toolbox">
<div class="left-toolbox">
<ul class="toolbox-list">
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use>
</svg><span class="name">点赞</span>
<span class="count"></span>
</a></li>
<li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use>
</svg><span class="name">收藏</span></a></li>
<li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{"mod":"1582594662_002"}"><svg class="icon" aria-hidden="true">
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use>
</svg>分享</a></li>
<!--打赏开始-->
<!--打赏结束-->
<li class="tool-item tool-more">
<a>
<svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
</a>
<ul class="more-box">
<li class="item"><a class="article-report">文章举报</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="person-messagebox">
<div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
<img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
<img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
</a></div>
<div class="middle-message">
<div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{"mod":"popu_379"}" target="_blank">解启超</a></span>
</div>
<div class="text"><span>发布了354 篇原创文章</span> · <span>获赞 52</span> · <span>访问量 3万+</span></div>
</div>
<div class="right-message">
<a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
</a>
<a class="btn btn-sm attented bt-button personal-watch" data-report-click="{"mod":"popu_379"}">已关注</a>
</div>
</div>
</div>
</article>