Skip to end of metadata
Go to start of metadata

Prioritising Processes With Nice and Renice

Category: Getting Started &nbsp

Processes on a system all have a priority assigned to them which is referred to as it’s niceness. This is a concept that harks back to shared user systems where the priority of the process defined how nice you were being to other users and processes on the system.

This priority is used by the system to allocate CPU resources to the processes, a process with a higher priority receives more CPU time than processes with lower priority.  To make things confusing referencing things in terms of niceness means that you increase the nice value to reduce a process’ priority and decrease the nice value to increase the priority.

Generally on Linux, nice values range from -20 to 19 with 0 as the default nice value.  -20 is the least nice you can be and therefore the highest process priority, 19 is the nicest you can be and is the lowest process priority.

By default, users can only set processes with a higher than normal niceness, with the root user being the only user that can set low nice values. Though the /etc/security/limits.conf file can be edited to allow users or groups the ability to set niceness values to pre-specified limits.

The tools nice and renice have slightly different functions to achieve the same job. Nice is used when launching an application to set an initial nice value, renice is used once a process is running to adjust its niceness.

Here’s an example of compressing the contents of the www directory using nice…

 nice -n 19 tar cvzf webbackup.tgz /var/www

In this instance it’s using the maximum niceness value of 19, which means that the process will get the minimum CPU time. This will cause the tar process to take longer but means that other processes will be less likely to be affected by it.

Renice can be used to change the nice values of individual processes or a group’s or user’s processes. With the example below, we can set all the processes belonging to the user bob to a niceness of 19.

 renice 19 -u bob

Note the syntax difference that renice doesn’t require the -n flag to set the nice value whereas the nice command does.

Now let’s look at the tar example above, imagine you’ve set it going and then realised that it’s using too many system resources. First you can find out the process id using the ps command as follows

            ps aux | grep tar

From there you should get a response similar to the below…

bob      14706  3.6  0.2 724848 19848 ?         Sl   18:09   0:00 tar cvzf webbackup.tgz /var/www

The second value is the process id in the ps aux output, so in this case our process id is 14706. So that can be reniced thus…

   renice 19 -p 14706

There you have it, a fairly simple but handy guide to prioritising processes using nice and renice.  For the majority of modern Linux systems this is rarely needed as modern systems have powerful CPUs with plenty of resources, but it’s still possible to over-tax systems with backup processes and similar where increasing the niceness can help more critical processes run.

Save this article