Skip to content

Nice command on Linux (change priority)

The nice command in Linux adjusts a process’s available CPU time to higher or lower priority.

In English the word “nice” means “cool”. If the priority adjustment for a process is a positive number, it means that it is being cooler with the other programs reducing its priority.

If the adjustment is a negative number, it means that the program is being less cool, increasing its execution priority and leaving less CPU time left for other programs.

The possible priority adjustment ranges from —20 (highest priority/least cool) to 19 (coolest, least priority).

If no adjustment value is passed, the nice command will adjust the priority to +10, reducing the process execution time.

By default, all programs start with zero priority. Only the root administrator can set negative priorities (increased CPU share).

In this example, the updatedb command has lower execution priority:

$ nice updatedb &

In this example, the payroll command will be executed with higher priority.

$ nice —n —10 payroll

To check the priority of running processes, the ps command can be used. The NI column shows the priority:

$ ps -eo pid, ni, comm <br></br>PID NI COMMAND <br></br>1 0 systemd <br></br>2 0 kthreadd <br></br>4 -20 kworker/ 0:0 H <br></br>6 -20 mm_percpu_wq <br></br>(...) <br></br>30018 0 sshd <br></br> 30019 0 bash

Some system processes naturally have top priority (-20).

The top command can also be used to view the priority of processes, in the NI column:

Figure 54 - Top with NI column highlighted
![](https://learnlinux.com.br/editor/files/nice_CompT.jpg)Renice command on Linux -----------------------

The renice command adjusts the execution priority of running processes.

By default, the renice command takes the PID of a particular process as a parameter. The priority setting is an integer from —20 (highest priority) to +19 (execute anything before this process).

The most common options are:

  • -p: Get a PID to change its priority.
  • -u: Receives a username to change the priority of all processes running on this user.
  • -g: Receives a name from a group to change the priority of all processes belonging to this group.

In this example, the process number PID 987, PID 32, and all processes that daemon and root users own will have higher priority.

# renice -1 987 -u daemon root -p 32

Ordinary users can only change the priority of the processes they own, and only to lower the priority (from -20 to +19). Once the average user decreases the priority, they cannot normally return to the previous priority.

Only the root user can change the priority of any process, increasing or decreasing the priority.

In this example, the program with PID 30018 went from priority 0 to +2: ``` $ renice +2 30018 30018 (process ID) with old priority 0, new priority 2

If an ordinary user tries to increase priority, he is unable to:

$ renice +1 30018 renice: failed to set priority to 30018 (process ID): Permission denied

Only root can increase priority. In this example, the sudo command executes renice as root:

$ sudo renice 0 30018 30018 (process ID) with old priority 2, new priority 0

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Did you like it?
-----------------
Share