nice is used to set the nice level for new processes.
renice and top can be used to modify the nice level on an existing process.
Both ps and top can be used to report on nice levels.
After completing this section, students should be able to:
Launch processes with a nice level set.
Modify the nice level on a running process.
Report on nice levels for processes.
The nice levels for existing processes can be viewed in a number of different ways. Most process management tools (like gnome-system-monitor) already display the nice level by default, or can be configure to display the nice level.
Displaying nice levels with top
The top command can be used to interactively view (and
manage) processes. In a default configuration, top will
display two columns of interest to the nice level: NI with
the actual nice level, and PR, which displays the nice level
as mapped to a larger priority queue, with a nice level of
-20 mapping to a priority of 0 and a nice
level of +19 mapping to a priority of 39.
Displaying nice levels with ps
The ps command can also display nice levels for
processes, although it does not do so in most of its default output
formats. Users can request exactly the columns they want from
ps, however, and the name for the nice field is
nice.
The following example requests a list of all processes, with their pid, name, and nice level, sorted in descending order by nice level:
[student@desktopX ~]$ps axo pid,comm,nice --sort=-nicePID COMMAND NI 74 khugepaged 19 688 alsactl 19 1953 tracker-miner-f 19 73 ksmd 5 714 rtkit-daemon 1
Some processes might report a - as their nice level. These
processes are being run with a different scheduling policy, and will
almost certainly be considered a higher priority by the scheduler. It is
possible to display the scheduler policy by requesting the
cls field from ps. A TS in
this field indicates the process is run under SCHED_NORMAL
and can use nice levels; anything else means a different scheduler policy
is being used.
Whenever a process is started, it will normally inherit the nice level from
its parent. This means that when a process is started from the command line,
it will get the same nice level as the shell process that it was started
from. In most cases, this will result in new processes running with a nice
level of 0.
To start a process with a different nice level, both users and system
administrators can run their commands using the nice
tool. Without any other options, running nice
<COMMAND> will start
with a nice level
of <COMMAND>10. Other nice levels can be selected by using the
-n option to
the nice command. For example, to start the command
<NICELEVEL>dogecoinminer with a nice level of 15 and send
it to the background immediately, the following command can be used:
[student@desktopX ~]$nice -n 15 dogecoinminer &
Unprivileged users are only allowed to set a positive nice level
(0 to 19). Only root can set a
negative nice level (-20 to -1).
The nice level of an existing process can be changed from the command line using the renice command. The syntax for the renice command is as follows:
renice -n
<NICELEVEL> <PID>...
For example, to change the nice level of all
origami@home processes to -7, a system administrator
could use the following command (note that more than one PID can be
specified at once):
[root@desktopX ~]#renice -n -7 $(pgrep origami@home)
Regular users are only allowed to raise the nice
level on their processes. Only root can use
renice to lower the nice level.
The top command can also be used to (interactively) change the nice level on a process. From within top, press r, followed by the PID to be changed and the new nice level.
nice(1), renice(1), and top(1) man pages