In GNU/Linux systems, "niceness" is used to define the CPU priority of a process. Essentially, it is the opposite of priority. So the nicer a process is, the less priority it has and vice versa. It is useful to adjust the niceness of a process, for example, when a non-important program is hindering CPU performance. By default, all processes have a niceness of 0. The niceness scale goes from -20 to 19.
As a non-root user, you can only increase niceness of processes you own. Root privileges are required to decrease the niceness of any process.
In this article, I will assume that you have root privileges (either logged in as the root user, or using sudo
).
Show niceness of a process
In htop
and top
, niceness is listed for each process under the "NI
" field:
root@demo:~# top
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 56892 6580 5216 S 0.0 1.3 0:01.09 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
3 root 20 0 0 0 0 S 0.0 0.0 0:00.00 ksoftirqd/0
5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
7 root 20 0 0 0 0 S 0.0 0.0 0:00.07 rcu_sched
Start a process with a specific niceness value
You can use the nice
command to execute a command with a niceness value of your choice. For example:
nice -n 10 apt upgrade
This would run apt upgrade
but with a niceness of 10, instead of the default value of 0.
The general format of the command above is as follows:
nice -n NICENESS COMMAND
Where NICENESS
is any number between -20 and 19, and COMMAND
is any command you would normally type in a shell.
Change niceness of a process
To modify the nice value of a process that is already running you would use renice
. For example:
root@demo:~# renice -10 -p 564
564 (process ID) old priority 0, new priority -10
This command re-assigns a nice value of -10 to the process with the PID 564
. The general format is as follows:
renice NICENESS -p PID
Where NICENESS
is any number between -20 and 19, and PID
is the process ID of the targeted process.