默认情况下进程的优先级都是0。有3个方法调整优先级
1. 在进程运行之前设置优先级
用的是nice命令
SYNOPSIS nice [OPTION] [COMMAND [ARG]...] DESCRIPTION Run COMMAND with an adjusted niceness, which affects process scheduling. With no COMMAND, print the current niceness. Nicenesses range from -20 (most favorable scheduling) to 19 (least favorable). -n, --adjustment=N add integer N to the niceness (default 10)
命令用法很简单,就是nice -n 优先级 进程启动指令。例如nice -n 10 ssh -l coosh 192.168.5.1,意思是ssh -l coosh 192.168.5.1这条指令以优先级10来运行。需要注意的是,优先级的取值范围是-20~19,数值越小越优先。
2. 在进程运行时修改优先级
用的是renice命令
SYNOPSIS renice [-n] priority [[-p] pid ...] [[-g] pgrp ...] [[-u] user ...] renice -h | -v DESCRIPTION Renice alters the scheduling priority of one or more running processes. The following who parameters are interpreted as process ID’s, process group ID’s, or user names. Renice’ing a process group causes all processes in the process group to have their scheduling priority altered. Renice’ing a user causes all processes owned by the user to have their scheduling priority altered. By default, the processes to be affected are specified by their process ID’s. Options supported by renice: -n, --priority The scheduling priority of the process, process group, or user. -g, --pgrp Force who parameters to be interpreted as process group ID’s. -u, --user Force the who parameters to be interpreted as user names. -p, --pid Resets the who interpretation to be (the default) process ID’s. -v, --version Print version. -h, --help Print help. For example, renice +1 987 -u daemon root -p 32 would change the priority of process ID’s 987 and 32, and all processes owned by users daemon and root. Users other than the super-user may only alter the priority of processes they own, and can only monotonically increase their ‘‘nice value’’ within the range 0 to PRIO_MAX (20). (This prevents overriding administrative fiats.) The super-user may alter the priority of any process and set the priority to any value in the range PRIO_MIN (-20) to PRIO_MAX. Useful priorities are: 20 (the affected processes will run only when nothing else in the system wants to), 0 (the ‘‘base’’ scheduling priority), anything negative (to make things go very fast).
这个命令可以根据pid来修改优先级,也可以根据运行进程的用户或组来修改。
[root@vmtest backup]# renice -n -1 -p $(pidof mysqld) -u coosh
7570: old priority 0, new priority -1
6828: old priority 0, new priority -1
6085: old priority 0, new priority -1
500: old priority 0, new priority -1
然后用top查看一下,可以看到NI列已经修改
[root@vmtest backup]# top -u mysql PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6085 mysql 19 -1 214m 19m 4268 S 0.0 1.9 0:28.46 mysqld 6828 mysql 19 -1 213m 19m 4252 S 0.0 1.9 0:27.36 mysqld 7570 mysql 19 -1 216m 21m 5672 S 0.0 2.1 0:27.47 mysqld
3. 利用top来修改
在top里点击r,会有个提示PID to renice,这里我输入6828
top - 10:24:20 up 1 day, 16:59, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 142 total, 1 running, 141 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.7%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1030720k total, 305792k used, 724928k free, 67620k buffers Swap: 1535992k total, 0k used, 1535992k free, 94648k cached PID to renice: 6828 <<这里输入pid PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6085 mysql 19 -1 214m 19m 4268 S 0.0 1.9 0:28.54 mysqld 6828 mysql 19 -1 213m 19m 4252 S 0.0 1.9 0:27.45 mysqld 7570 mysql 19 -1 216m 21m 5672 S 0.0 2.1 0:27.57 mysqld top - 10:24:20 up 1 day, 16:59, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 142 total, 1 running, 141 sleeping, 0 stopped, 0 zombie Cpu(s): 0.1%us, 0.0%sy, 0.0%ni, 99.7%id, 0.1%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1030720k total, 305792k used, 724928k free, 67620k buffers Swap: 1535992k total, 0k used, 1535992k free, 94648k cached Renice PID 6828 to value: 1 <<输入新的优先级 top - 10:26:16 up 1 day, 17:00, 1 user, load average: 0.00, 0.00, 0.00 Tasks: 142 total, 1 running, 141 sleeping, 0 stopped, 0 zombie Cpu(s): 0.0%us, 0.2%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 1030720k total, 305800k used, 724920k free, 67644k buffers Swap: 1535992k total, 0k used, 1535992k free, 94648k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6085 mysql 19 -1 214m 19m 4268 S 0.0 1.9 0:28.57 mysqld 6828 mysql 21 1 213m 19m 4252 S 0.0 1.9 0:27.48 mysqld 7570 mysql 19 -1 216m 21m 5672 S 0.0 2.1 0:27.58 mysqld
立即看到变化