Updated April 14, 2023
Introduction to Linux Nice Command
In Linux, the nice command is a way to run or start a process with a value. Now the question which arises is what importance does this value plays in the execution of the process. To understand this, we need to first consider that each process that needs to be executed requires a space in the RAM and CPU time. Now, if there are unlimited processes, one would need to set up the priority of execution. This priority is what is decided by this nice number. So, in short, the command helps in attaching a number to the execution of a process that decides the priority of execution.
Explain the Linux nice command
The nice command has utility in one of the niche areas in process execution. Now in any operating system, when we start to work and open so many applications, the operating system cannot handle it because of the restriction of limited RAM to process the same. In this case, what will strike in our mind is to suspend some of the processes and start them later when the execution power is available. Now, how do we decide on what jobs need to be suspended and which one needs to be kept as processing? Over here is the point of putting a priority list of the important process needed for the execution.
For example, we would never give low priority to a process that handles the connectivity of the system with the internet and neither to the ones that form as the operating system’s baseline. In giving low priority to those processes, one would expose themselves to chances of malfunctioning the entire operating system or any essential services. This is where nice command comes in handy to provide a nice number which comes high in the priority. Now the next question is how do we proceed with assigning a nice number or, in other words, a priority number?
Here goes the syntax
nice -n <number><process name>
Now one thing which needs to be kept in mind is that the <number> can vary from -20 to 19. To understand what each magnitude means lesser the number, the more priority it is. In this way, a nice value or number of -20 is the process with the highest priority, and the one with 19 is the process with the least priority. The next important thing is that the nice command only controls the time assigned to a process in the CPU. It doesn’t take care of prioritizing in terms of memory utilization or I/O devices utilization.
Once you put a nice number corresponding to a process, the operating system will make sure that the particular process is running at a high priority without even a chance of it lagging.
The next question that might arise is if one needs to de-prioritize the nice number, what should one do. Is it possible? The answer is yes! There is another command which takes care of de-prioritizing the nice number and the syntax go as:
renice value <Process ID>
renice is nothing but the command that will call changing the priority with upgrading the priority or de-prioritizing. Value denotes the nice number which needs to be assigned to the process, and process id is the id under which the process is running.
As an example, the syntax goes as:
renice -n 15 -p 9999
This essentially means that a process with process id as 9999 is set to 15, which eventually could have been any other number as a nice number. The next thing one should also be aware of is that by default, the nice value to any process in execution is set to 10.
Some other options can be used for changing priority for renice, and some other available options for nice commands. The following are they:
- Changing priority number for all processes under a group for renice: One would use the option -g <group name> to change the priority for all processes under a group. The group which is mentioned will lead to having the same nice number for all the processes under that.
- Changing priority number for all processes under a user for renice: One would use the option -u <name of user> to change the priority for all processes under a user under a user. The user mentioned in the command will lead to having the same nice number for all the processes under that.
- –help: This option acts as a message which would be required in case one gets stuck with the syntax related queries
- –version: In some cases, one would see that some options are working in one PC and the same doesn’t work in another, which essentially means that there might be some mismatch in the version between both the nice command installed. Using this option in both PCs will let us know if the difference in version is causing the problem.
Examples of Linux Nice Command
It is now time for us to look at all the options we encountered in the article and take a hands-on experience of them!
Understanding the different processes having different NI numbers:
Changing the NI of bash to 5 in terms of nice value.
nice -n 5 bash
Here you can realize that a bash process has started with process id 80 and a nice value of 5.
Changing the nice value (NI) of bash to -12from -5 for process id 80.
renice -n -12-p 80
Here you can realize that the nice(NI)value has been changed to -12 from -5
In this article, we have gone through the usage of nice command in real life through an example and also some concepts on how we can change the priority by either prioritizing or de-prioritizing. In using this capability, one can easily mold the execution of any process to the way the admin of Linux wants to!
We hope that this EDUCBA information on “Linux Nice Command” was beneficial to you. You can view EDUCBA’s recommended articles for more information.