Nice and Renice Command – How to Set Process Priority in Linux ?

Process priority is the one that directly impacts the priority of the schedule of the processes. All data related to processes and process priority are stored on the kernel. Such priorities can be defined manually and are assigned with a range value.

The kernel loads and executes the process that has a higher number of priorities first rather than the process with a low number of priorities. Such priorities can be set through nice and renice commands in Linux.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux System command queries.


What is the Nice Command in Linux ?

Nice is used to set the priority value so the process can be executed accordingly. The value for nice is set to -20 as the higher number of priority values and 19 as the lowest number of priority values. So processes with higher priority can be executed first.

Let's check the priority status with the nice value of htop. You can run the below command:

$ ps -el | grep htop

You can see the nice value for htop is 0 for now. You can also check those stats with the "top" or "htop" command on Linux. To change the nice value, you can change the nice value as per below command:

$ nice -19 htop

Now check the nice value of htop with the following command:

$ ps -el | grep htop

Note: When you want to set higher priority then you have to run commands with sudo privilege user. Here, when we tried to set a nice value for higher priority, permission denied is shown. But using sudo executes the command successfully.

$ sudo nice --5 htop


What is the Renice Command in Linux ?

A nice command is used to set priority higher or lower for any process. But renice is used to set the priority higher or lower for the running process. It can set priority without stopping the process.

Using the ps command, you get the pid of the required process. Using grep for the specific process. In our case, we are executing the ps command for the pid of the htop process. See the screenshot below for further details:

$ ps -el | grep htop

With the above command, you have noticed pid, a nice value for the htop process. Now with these details, we can use renice command to change the priority of the running process as per the below command:

$ renice -n 10 -p 2257

Here, the running process with the same PID has new priority 10 set by the renice command for the process htop. So, you can easily change the priority with the renice command with the pid of the running process.


[Need help to fix Linux systems issues ? We can help you. ]

This article covers more about the nice and renice command in Linux. In fact, If you need to set such priority for the specific process for the kernel to load and execute the process with the correct management of CPU resources, these nice and renice come in handy. 

Related Posts