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:
In this example, the payroll command will be executed with higher priority.
To check the priority of running processes, the ps command can be used. The NI column shows the priority:
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 |
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.
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
$ renice +1 30018 renice: failed to set priority to 30018 (process ID): Permission denied
$ sudo renice 0 30018 30018 (process ID) with old priority 2, new priority 0