Using Nice With Your Linux Processes
While modern multi-tasking operating systems can make it appear that all your applications are running at the same time, the truth of the matter is somewhat different. Computer multitasking works using a method called time slicing, processes are assigned a slice of CPU time by the operating system during which they are processed. The processes themselves have no control over the CPU time that they get as this is all handled by the kernel, the kernel’s process scheduling can be influenced by the user though. This is done with the process’s nice value.
What is a Nice value?
The kernel uses the process’s nice value to decide how to prioritize its CPU time. Nice values range from -20 (maximum priority for the process) to 19 (least priority for the process). The default nice value for a new process is 0, reducing this to a negative number increases the amount of time that the processor assigns to the process at the expense of other processes. Increasing that number to a positive number will mean that other processes will be run at the expense of that process.
There are two commands that can be used to affect the nice value of a process: nice and renice. We’ll start by looking at nice.
Nice
The nice command is used to set the initial nice value of a process. It can also be used to view the current default nice value. The use is fairly simple – the command on its own works as follows:
nice
This will provide a simple number for the default nice value. Unless you have manually changed it this will show as 0. To start a command it takes a simple flag -n which sets the nice value to be the default nice value added to the value given and is followed by the command to be run. For example:
nice -n10 nano file.txt
This uses nano to open the file file.txt and gives nano’s process a nice value of 10 if the default value is 0. If the default is 3 then the nice value of the process would be 13. While any user can set a higher nice level to a process, to set a nice value that is negative to the default (increasing the priority of the process) requires superuser access.
sudo nice -n-10 nano file.txt
Renice
The renice command, on the other hand, is used to change the nice value of an already running process or processes. It works in a fairly similar manner to nice, only this time the -n flag adjusts the nice value relative to the current nice value of the process. There are three ways of selecting processes to adjust and each has its own flag. Something to note is that you can only change the nice value of processes that belong to your use unless you are using superuser privileges.
-p Select process by its process id
-g Select processes by the running group id
-u Select processes by the running user id
Here are some examples:
sudo renice -n -10 -p 2343
This command adjusts the nice value of the process with pid 2343 to -10.
sudo renice -n 5 -g users
This command adjusts the nice value of all processes with the group “users” to 5.
sudo renice -n 15 -u hayden
This command adjusts the nice value of all processes belonging to the user hayden to 15.
In most normal situations you would have little need to adjust the nice values of processes on your system. However, they are helpful to know in case you need to run any non-essential CPU intensive tasks, increasing the nice value of the task can allow the rest of the system to function normally while the task takes place at a slower pace.