shell高级技巧-改变进程优先级的命令(nice和renice)

该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
发布了350 篇原创文章 · 获赞 52 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/xie_qi_chao/article/details/105039315