Linux nice Command: Adjusting Process Execution Priority
The nice command in Linux adjusts the available CPU time of a process to a higher or lower priority.
In English, the word "nice" means "kind" or "friendly". If the priority adjustment for a process is a positive number, it means it is being nicer to other programs by decreasing its priority.
If the adjustment is a negative number, it means the program is being less nice, increasing its execution priority and leaving less CPU time for other programs.
The possible priority adjustment ranges from -20 (highest priority / least nice) to 19 (nicest, lowest priority).
If no adjustment value is passed, the nice command will set the priority to +10, decreasing the process execution time.
By default, all programs start with zero priority. Only the root administrator can set negative priorities (increased CPU slice).
In this example, the updatedb command has lower execution priority:
$ nice updatedb &
In this example, the folha_pagamento command will be executed with higher priority.
$ nice –n –10 folha_pagamento
To check the priority of running processes, the ps command can be used. The NI column shows the priority:
$ ps -eo pid,ni,comm PID NI COMMAND 1 0 systemd 2 0 kthreadd 4 -20 kworker/0:0H 6 -20 mm_percpu_wq ( ... )30018 0 sshd30019 0 bash
Some system processes naturally have maximum priority (-20).
The top command can also be used to view process priorities, in the NI column:
Figure 54 - Top with NI column highlighted |
renice Command in Linux
The renice command adjusts the execution priority of running processes.
By default, the renice command takes the PID of a given process as a parameter. The priority adjustment is an integer from -20 (highest priority) to +19 (run anything before this process).
The most common options are:
- -p: Takes a PID to change its priority.
- -u: Takes a username to change the priority of all running processes of that user.
- -g: Takes a group name to change the priority of all processes belonging to that group.
In this example, process number PID 987, PID 32, and all processes owned by users daemon and root will have higher priority.
# renice -1 987 -u daemon root -p 32
Regular users can only change the priority of processes they own, and only to lower the priority (from -20 to +19). Once a regular user lowers the priority, they normally cannot go back to the previous priority.
Only the root user can change the priority of any process, either increasing or decreasing the priority.
In this example, the program with PID 30018 went from priority 0 to +2:
$ renice +2 3001830018 (process ID) com prioridade antiga 0, prioridade nova 2
If a regular user tries to increase the priority, they cannot:
$ renice +1 30018renice: falha ao definir prioridade pra 30018 (process ID): Permission denied
Only root can increase priority. In this example, the sudo command executes renice as root:
$ sudo renice 0 3001830018 (process ID) com prioridade antiga 2, prioridade nova 0
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.
Figure 54 - Top with NI column highlighted