nice, renice & ionice

The tools nice, renice and ionice are used to adjust the scheduling of a process giving it a higher or lower priority than normal. Run without arguments, nice just shows the niceness level of the current process which by default should be 0:

root:~> nice
0

The levels of niceness go from -20 (the least nice or nastiest) to +19 (the nicest). The more negative the figure the more resources the process will use and the fastest it will complete. The opposite happens with “positive niceness”: less resources used per time interval and more time required to complete.

Let’s see some examples of use:

root:~> nice batchjob.sh       → increases the niceness by +10 (lower priority)
root:~> nice ­-n 19 batch.sh    → increases the niceness to +19 (lower priority)
root:~> nice ­-n ­20 batch.sh    → decreases the niceness to ­20 (highest priority)

Non-privileged users can only set niceness to positive values (lower priority than normal). Only root can set negative niceness values and thus higher-than-normal priority.

Once a process is running we cannot “nice” it but we can renice it:

root:~> renice -­n +1 ­-p 4564 5566 6576 4535     → renices +1 all 4 processes
root:~> renice +1 ­-p 4564 5566 6576 4535        → same as above
root:~> renice +1 4564 5566 6576 4535           → same as above
root:~> renice +1 ­-u marc john matt             → renices +1 all processes owned by those 3 users
root:~> renice +1 ­-g 45 770                     → renices +1 both process leaders and all children

The ionice command is used to query and set the I/O class scheduling and priority. There are three I/O scheduling classes:

best­-effort (class 0 or 2) is the class by default. Processes in this class with the same priority get served in a round-­robin fashion the same amount of resources. There are 8 priorities within this class: 0 (highest priority) up to 7 (lowest).

real-time (class 1) processes get first access to disk. Only root can set real-time I/O class to processes and it also has 8 priority levels.

idle (class 3) processes only get access to disk when no other process in best­ effort or realtime class needs it. There are no priorities in this class.

Let’s query the class:prio of a PID  

root:~> ionice -­p 2569
none: prio 0               → it has class none or best-effort and priority 0

We’ll keep the PID above in the same class but will increase priority to 0 or highest:

root:~> ionice ­-c 2 -­n 0 ­-p 2569 

Now we will change to real-time a few processes:

root:~> ionice ­-c 1 -­n 4 -­p 453 456 470

We need to run a batch job but time completion is not important so we’ll set it to idle class:

root:~> nohup ionice ­-c 3 batchjob9.sh &

We now want user daemon processes to be switched to real-time:

root:~> ionice ­-c 1 -­n 4 ­-u daemon

Finally we need to change the class to real-time for all processes in process group 2565:

root:~> ionice ­-c 1 -­n 4 -­g 2565

 

<< kill, pkill & killall                      lsof >>