At any given time, a Linux system has hundreds of processes running, most of which were started by the Linux operating system and some of which were started by the person who is now signed in.
In this tutorial, we will learn about process priorities and how we can tweak the priorities with nice command in Linux.
What is nice command
The nice command is a great way to manage process priorities in Linux. By default, processes run at a niceness of 0. In nice command, if the number is low then it certainly has the highest priority. You can use nice to change the priority of a process on-the-fly.
Priority settings are set for Linux processes before they are launched using the nice command. Using the nice command to adjust the priority after it has been set cannot be done. The renice command can be used in this situation. An already active process's priority can be changed with the renice command.
Installation
Linux nice command comes pre-installed with the package - "coreutils". You might try installing the following package depending on the distribution you choose.
Debian (Ubuntu, Kali Linux, Raspbian, etc)
sudo apt install coreutils
Arch Linux (Manjaro, Garuda, etc)
pacman -S coreutils
Redhat / CentOS Stream
yum install coreutils
Fedora
dnf install coreutils
Alpine
apk add coreutils
macOS X
brew install coreutils
Syntax
nice [options] [command [args]...]
Nice command options are:
Options | Description |
---|---|
-n, --adjustment=N | N-fold increase in niceness (default 10) |
--help | Display the help menu |
--version | print the version information, then quit |
Important to note
Nice defaults to a priority of 10 if no value is specified. A command or programme that is run without nice has a priority of zero. Only root has the ability to launch commands or programmes with elevated or high priority. Normal users can only launch low-priority commands or programmes.
Nice priority
The kernel holds a lot of information about processes, including process priority, which is simply the scheduling priority assigned to a process. Processes having a higher priority are executed before those with a lower priority, whereas processes with the same priority are scheduled one after the other, repeatedly.
In Linux, 140 priorities and two separate priority ranges are implemented. The first is a nice number (niceness), which goes from -20 (highest priority value) to 19 (lowest priority value), with 0 being the default, as we will see further in the article. The other is the real-time priority, which by default spans from 1 to 99, with 100 to 139 reserved for user space.
How to check nice value of a process
There are several ways to view the nice value of processes. We can use programmes like ps, top, or htop to see the good values of processes.
To view the nice value of running processes using the ps command -
ps -l
In the 8th column, you see "NI" is the nice value of the already running process.
To display processes' nice values in user-defined format, just use the ps command in this manner -
ps -eo pid,ppid,ni,comm
The 3rd column "NI" is the nice value for each process. Other information is being displayed like process id, parent process id and command.
Now, let us see the output using the top command -
top
Here the 3rd column shows the nice values. Also, other information gets displayed which might help you in your task.
The same thing using the htop command -
htop
htop command displays the output in a colourful and understandable manner. It comes with many more resources you can try.
Linux process priority vs nice
You'll note in the top and htop outputs that there is a column named PR and PRI receptively that displays the priority of a process. Here PR and PRI are the actual priority values set by the Linux kernel whereas the NI column is the niceness value and is a user space(user-defined) value.
Nice Command Examples
Let's look at some examples of how to use nice command. This will give you a better understanding of how this tool work.
Example 1: Set the priority of a process
Here, we'll look at how to give a command or application priority when it comes to CPU utilisation. Using the nice command, you can give a job or programme a high or favourable priority if it uses a lot of CPU resources and you know it might take a while to finish.
sudo nice -5 tar -czf linuxuser.tar.gz ./Documents/*
The priority of a running process cannot be altered by the nice command. However, you can start any application with a specified priority by using the nice command.
To change the scheduling priority of an already running process using the renice command -
renice -n -12 -p 1055 renice -n -2 -u apache
Any modifications you make to a user's process' nice values using the renice command are only effective up until the following reboot.
Example 2: Set the negative priority for a process
As was said in the introduction, niceness values vary from -20 to 19, with the first value being the most pleasant and the last being the least. You must use a double hyphen if you want to attach a negative nice value to the process.
sudo nice --10 ./some-program
Example 3: Add priority to task permanently
In the /etc/security/limits.conf file, you can change a user or group's default nice value. Its main job is to set resource restrictions for PAM-logged in users.
Syntax
<username> <hard|soft> priority <nice value>
Create the linux-priority.conf file under /etc/security/limits.d for the linuxuser user.
sudo gedit /etc/security/limits.d/linuxuser-priority.conf
Add this configuration after that -
linuxuser hard priority 10
Save the document, then exit. Every process that linuxuser owns going forward will have a nice value of 10 and a PR of 30.
Read the nice man page for additional details by running the following command:
man nice
Conclusion
In this post, we have demonstrated how to utilise the nice command. Linux changes the priority of a certain process by using the nice value. By setting a positive pleasant value, you are reducing that process' priority. However, a low pleasant value raises that process's priority. You can alter the priority of currently running programmes and processes with the Renice command. Both of these tools are quite effective since they have a direct impact on how the scheduler will give particular tasks priority.
Navigate all-in-one place of Linux Commands for more learning.
Comments