Linux下nice/renice命令小结

1. nice命令 

 

内核根据进程的nice值决定进程需要多少处理器时间. nice值的取值范围是是: -20到20. 一个具有-20 的 nice 值的进程有很高的优先级. 一个 nice 值为 20 的进程的优先级则很低.  

 

1) 用 ps axl 显示所有正在运行的进程的 nice 值  

 

# ps axl 

F   UID   PID PPID PRI NI     VSZ RSS WCHAN STAT TTY TIME COMMAND 

4     0     1     0 16    0 2172 552 -        S    ? 0:17 init [5] 

1     0     3     1 34 19       0    0 ksofti SN   ? 3:18 [ksoftirqd/0] 

1     0    10     1   5 -10     0    0 worker S<   ? 0:01 [events/0] 

4     0 5145      1 25 10 32124 18592 -       SNs ? 0:08 /usr/bin/python /usr/bin/rhn-applet-gui --sm-client-id default4 

4     0 5147 5142 16      0 3528 604 -        S    ? 0:00 /sbin/pam_timestamp_check -d root 

1   503 17552 4180 16     0 14208 3920 -      S    ? 0:01 /home/www/apache2/bin/httpd -f /home/www/apache2/conf/httpd.conf -k start 

 

2) 如何给一个shell脚本分配一个低的优先级(更高的nice值)?  

 

在下面的例子里, 当我在后台启动 nice-test.sh 脚本, nice 值为 0.  

$ ./nice-test.sh & 

[3] 13009 

$ ps axl | grep nice-test 

0   509 13009 12863 17    0 4652 972 wait S pts/1 0:00 /bin/bash ./nice-test.sh 

[注: 第六列数值为 0 的是 nice 值]  

现在, 以不同的 nice 值来执行相同的脚本, 如下所示: 

$ nice -10 ./nice-test.sh & 

[1] 13016 

$ ps axl | grep nice-test 

0   509 13016 12863 30 10 4236 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh 

[注: 第六列数值为 10 的是该 shell 脚本的 nice 值] 

 

3) 如何给一个shell脚本分配一个高的优先级(更低的nice值)?  

 

下面的例子里, 分配给 shell 脚本 nice-test.sh 一个"-10"的 nice 值.   

$ nice --10 ./nice-test.sh & 

[1] 13021 

$ nice: cannot set priority: Permission denied 

注意: 只有 root 用户可以设置一个负的 nice 值. 用 root 用户登录再次尝试. 注意在下面的nice 命令里的 10 前面有一个双破折号.   

# nice --10 ./nice-test.sh & 

[1] 13060 

# ps axl | grep nice-test 

4     0 13060 13024 10 -10 5388 964 wait S< pts/1      0:00 /bin/bash ./nice-test.sh 

[注: 第六列数值为-10 的是该 shell 脚本的 nice 值] 

 

 

2. renice命令 

 

renice 可以给正在运行的进程设置调度优先级.  

 

1) 怎样降低一个运行进程的优先级(增加 nice值)?  

 

下面的例子里, 一个存在的 shell 脚本运行时的 nice 值为 10. (ps 输出的第 6 列) 

$ ps axl | grep nice-test 

0   509 13245 13216 30 10 5244 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh 

为增加 nice 值(因此降低优先级), 按如下方法执行 renice 命令.  

$ renice 16 -p 13245 

13245: old priority 10, new priority 16 

$ ps axl | grep nice-test

0   509 13245 13216 36 16 5244 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh 

[注: 现在, nice-test.sh (PID 13245)的第 6 列显示新的 nice 值是 16] 

 

2) 怎样增加运行进程的优先级(减少 nice值)? 

 

下面的例子里, 一个已有的 shell 脚本运行时 nice 值为 10. (ps 输出第 6 列)  

$ ps axl | grep nice-test 

0   509 13254 13216 30 10 4412 968 wait  SN pts/1      0:00 /bin/bash ./nice-test.sh  

为提升其优先级, 赋予其一个较低的nice值. 然而, 只有root可以提升运行进程的优先级, 否则你会得到以下错误信息.  

$ renice 5 -p 13254 

renice: 13254: setpriority: Permission denied 

Login as root to increase the priority of a running 

process 

$ su - 

# renice 5 -p 13254 

13254: old priority 10, new priority 5 

# ps axl | grep nice-test 

0   509 13254 13216 25    5 4412 968 wait     SN pts/1      0:00 /bin/bash ./nice-test.sh 

注: 第 6 列现在显示了一个较低的 nice 值 5(提升的优先级)]

转载于:https://www.cnblogs.com/oskb/p/3865993.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值