What is nice and how to change the priority of any process in Linux?

If you want to change the priority of any process there are two things which you need to consider. There are two terms which will be used in this article i.e. NICE and PRIORITY.
In case you haven't notice when you run top command you get two different values for any process as I have marked in different color below

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2899 root      20   0  2704 1128  868 R  0.3  0.1   0:02.26 top
    1 root      20   0  2892 1420 1200 S  0.0  0.1   0:01.29 init

Here PR denoted PRIORITY and NI denotes NICE value where the PRIORITY range varies from 0 to 39 for any process in Linux and NICE value varies from -20 to 19.
`nice' prints or modifies a process's "niceness", a parameter that affects whether the process is scheduled favorably.
Syntax

# nice [OPTION] [COMMAND [ARG]...]


Example:
The below command will give a nice value of -20 to 2342 PID

# nice --20 2342

NOTE: Use (--) to give a negative nice value and (-) to give a positive nice value
The below command will give a nice value of 20 to 2342 PID

# nice -20 2342

Below comes the complicated part so please bear with me.
 
How do I understand from the NICE value about the priority of the process?
If you consider to look after NICE value to determine the priority of the process then as I said above its value ranges from -20 to 19 where

  • -20 (process has high priority and gets more resources, thus slowing down other processes)
  • 19 (process has lower priority and runs slowly itself, but has less impact on the speed of other running processes)

So in case you want any process to be given high priority (considering the fact that other processes might get slow) you can change their priority to any negative value upto -20 which will decrease the execution time of the process and the process will complete faster comparatively.
Let us see some real time examples
Run a process with nice value as -20

# time nice --20 seq 4234567 > file.txt
real    0m2.572s
user    0m2.519s
sys     0m0.047s

Deleted the file and ran the same process with nice value of +20

# time nice -20 seq 4234567 > file.txt
real    0m2.693s
user    0m2.626s
sys     0m0.059s

As you can see the former command executed faster with a negative nice value.

Value Description
real It represents time taken by command to execute since its initiation to its termination
user It represents the amount of time that command/program took to execute its own code
sys It represents time taken by Unix to fire the command


How do I understand from the PRIORITY value about the priority of the process?
Again in case you consider to look at PR value for understanding the priority of the process the value ranges from 0 to 39 where

  • 0 (process has high priority and gets more resources, thus slowing down other processes)
  • 39 (process has lower priority and runs slowly itself, but has less impact on the speed of other running processes)

Let us see some real time examples

Run a process with positive nice value

# time nice -20 seq 42345671 > file.txt
real    0m27.548s
user    0m26.091s
sys     0m1.004s

As you see we are running a process with nice value +20 for which the NI appears 19 and PR as 39 which means the process will have the least priority and it will give priority to other process with higher nice value to use the system resources for their execution.

# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3192 root      39  19  4084  568  512 R 99.8  0.1   0:03.29 seq
    1 root      20   0  2892 1420 1200 S  0.0  0.1   0:01.29 init

Similarly for a negative nice value

# time nice --20 seq 42345671 > file.txt
real    0m27.397s
user    0m26.555s
sys     0m0.600s

As you see the NI value is changed to -20 for PR value of 0.

# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3205 root       0 -20  4084  568  512 R 75.0  0.1   0:02.26 seq
    1 root      20   0  2892 1420 1200 S  0.0  0.1   0:01.29 init


What would happen if I give a nice value out of range i.e. -20 to 19 to any process?
It can happen but your system won't understand any value other than -20 to 19 and will take the default value of -20 for high priority and 19 for least priority.
Let us see some real time examples
Assigning a nice value of -40

# time nice --40 seq 42345671 > file.txt

But still as you see the system is taking the nice value as -20 which is the highest recognizable value

# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3226 root       0 -20  4084  568  512 R  7.6  0.1   0:00.23 seq
 1600 root      20   0 38616 3976 3284 S  0.3  0.4   0:01.68 vmtoolsd

Assigning a nice value of 40

# time nice -40 seq 42345671 > file.txt

Naah, it didn't worked either as top shows the process is using 19 as the nice value.

# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3235 root      39  19  4084  568  512 R 62.0  0.1   0:01.87 seq
 2899 root      20   0  2704 1128  868 R  0.7  0.1   0:08.66 top

So I guess I made my point.
If you want to manually check the nice value along with the CPU and memory usage bu any process then use this command

# ps -o pid,pcpu,pmem,ni -p 88
 PID %CPU %MEM  NI
  88  0.0  0.0 -10

This will show you PID, %CPU,%MEM and nice value along with the process ID where 88 is the process id.
 
How to change the nice value of a running process?
In the above examples I started the process with a pre-defined nice value but what if the process is running and you want to change its nice value. For this we have another command i.e. renice
Syntax

# renice [-n] priority [[-p] pid ...]

Some Examples:

# seq 4234567112 > file.txt
# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3354 root      20   0  4084  568  512 R 95.4  0.1   0:07.19 seq

Changing the nice value to -5

# renice -n -5 -p 3354
3354: old priority 0, new priority -5
# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3354 root      15  -5  4084  568  512 R 99.7  0.1   0:30.26 seq

Changing the nice value to 10

# renice -n 10 -p 3354
3354: old priority -5, new priority 10
# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3354 root      30  10  4084  568  512 R 99.1  0.1   0:51.16 seq

Changing the nice value to -15

# renice -n -15 -p 3354
3354: old priority 10, new priority -15
# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 3354 root       5 -15  4084  568  512 R 98.0  0.1   1:12.04 seq

 

How to change the nice value of any user?
Suppose you do not want a particular user to use much of your system resource, I thoses cases you can assign low nice value so that every process started by that user will use less system resources.

# renice -n 5 -u deepak
500: old priority 0, new priority 5

Execute a process by user "deepak"

[deepak@test ~]$ seq 12345678 > file.txt

Verify the NI value for "deepak"

# top
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 4414 deepak    25   5  4084  588  532 R 97.3  0.1   0:05.54 seq


How to change the nice value of any user?
The same magic can be done for any particular group as well using the below command

# renice -n 5 -g work

The above command will change the default nice value of "work" group to 5 for any process running under their ownership.
 
What is the default NI value for any process?
The default NI value is 0 and PR value is 20 for any process running under Linux.
 
How to change the nice value for any user or group permanently?
The above shown examples are terminal based hence temporary. As soon as you reboot your machine the default nice value would be applicable for the defined user.
To make these changes permanent follow the below steps
NOTE: You can either user PR value or NI value to set the priority. I would suggest to use NICE value

# vi /etc/security/limits.conf
deepak hard priority 5

This will set hard priority for user deepak as "5"
 
Related Articles:
10 examples to help you understand top command usage in Unix/Linux
Tutorial for Monitoring Tools SAR and KSAR with examples in Linux
How to check all the currently running services in Linux
How to auto start service after reboot in Linux