How nice are your processes?

The top command has a row labelled “%Cpu(s)“. It looks like this:

%Cpu(s):  1.7 us,  0.6 sy,  0.0 ni, 97.1 id,  0.0 wa,  0.6 hi,  0.0 si,  0.0 st`

Here is what man top has to say about them:

us : time running un-niced user processes
sy : time running kernel processes
ni : time running niced user processes
id : time spent in the kernel idle handler
wa : time waiting for I/O completion
hi : time spent servicing hardware interrupts
si : time spent servicing software interrupts
st : time stolen from this vm by the hypervisor

What even is an un-niced process?

Basically, Linux has a concept of how “nice” a program is - a value that describes how much priority the process is requesting from the CPU scheduler. In other words, the “nicer” a program is, the less priority it requests, and therefore the more it allows other processes to do their thing (see wikipedia entry).

And this is incredible, because it means we can define the niceness of a background task, for example, such that we can continue working in the mean time. Consider the following: you run a media server, and you want to compress a bunch of your movies. This is a long term task, and you want to continue serving files in the mean time. You could some thing like this:

nice -n 19 compress file

In general, the higher the niceness value of a process, the less CPU priority it will require. Thus above the compress process would have a niceness of “19”, making it particularly nice (note that a negative value would be written as nice --19).

Renice

The niceness of a program is not set in stone. Assuming we have the long running command, but we need to change its niceness down the line we can use the renice command.

For example, we can run renice -n 6 <PID> to change lower the priority of the program with that PID. It should be noted that increasing the priority (that is to say, lowering the niceness) requires sudo privileges.