How to change the priority of a Linux process – What is nice value in Linux?
What actually “nice value” means ? Why should we bother ?
Every process which are running and waiting to execute under Unix has it’s own priority. Changing the priority comes under the management of server processor (CPU) time with respect to the processes. Simply the processor or CPU is like a human juggling multiple process at a time.
By considering the example of a human who is handling multiple process at a time, really it is very difficult to do without a proper planning or guidelines. However, it is very simple if he has a proper plan or priority for process which has to be done first, then what, and so on.
In the case of Unix/Linux, nice value of a process determines the priority of that process to CPU. Nice value is a guidelines to the CPU to follow when it is looking at all of the works it has to do.
The nice value is an integer value which is ranging from -20 to 19 and the default is “0“. Lesser the integer has high nice value or priority. Which means -20 has higher priority to server processor or CPU.
Nice range : -20 to 19 Default value : 0
How to find out the nice value of a process which is running under the server ?
Top displays the dynamic output and the PS shows a static result.
By using the command TOP.
See the output, there is one value in the result “NI” which is representing the Nice value of the process running under the server.
By using the command PS
For simplicity we can use PS with custom output.
# ps -o comm,nice -p PID
The above command will displays the Nice value of the process with that particular PID.
[root@server] # ps -o comm,nice -p 30594 COMMAND NI cpanellogd - sl 18
“nice” and “renice” commands to set and reset the Nice value of a process.
Setting priority on new processes with “nice” command.
Which means, we can start a process with our own costum nice value on it, that awesome 🙂 . Let’s see how it is possible.
Execute your command or process with custom priority using “nice” 🙂
Use the command nice to set its nice value:
# nice $NI Command
Here NI represents the nice value which we want to set.
# nice -20 ping localhost
I’ve selected the PING command to demonstrate the nice. Execute the above command under screen.
nice value details from PS
# ps aux |grep ping root 24728 0.0 0.0 8136 744 pts/3 SN+ 08:39 0:00 ping localhost -bash-3.2# ps -o comm,nice -p 24728 COMMAND NI ping -20
That’s it !
Resetting the priority for already existing Process.
You can change the nice value of a process which is already executing under your server by using the “renice” command.
# renice $NI -p PID
Here NI represents the new nice value.
# renice 0 -p 24728 24930: old priority 19, new priority 0 # ps -o comm,nice -p 24728 COMMAND NI ping 0
That’s it 🙂