renice commands let you fine-tune how the kernel treats your processes by adjusting their priorities. Read this tutorial to learn how to use them in Linux and Unix-like operating systems such as macOS.
It’s All a Matter of Process
Inside your Linux or Unix-like computer, there will be many processes running even before you launch the application you want to use. The majority of these processes are vital elements of Linux itself or supporting processes for your graphical desktop environment. There’s a lot going on behind the scenes. Of course, there are only so many system resources and CPU time to go around. The Linux kernel is the controller for all of these processes.
It is the kernel that has to decide which processes get attention and resources right now, and which ones have to wait. The kernel is continually juggling processes and priorities to ensure that the computer runs as smoothly as it can and that all processes get their appropriate share. Some processes get preferential treatment. They are so important to the general operation of the computer that their needs have to come first ahead of, say, your browser.
The nice Value
One of the criteria used to determine how the kernel treats a process is the nice value. Every process has a nice value. The nice value is an integer in the range of -19 to 20. All standard processes are launched with a nice value of zero.
The trick here is that the higher the nice value, the nicer your process is being to the other processes. In other words, a high nice value tells the kernel that this process is happy to wait. A negative number is the opposite of being nice. The larger the negative nice value, the more selfish the process is. It is trying to get as much CPU time as it can, with no regard for the other processes.
The nice Command
We can use the
nice command to adjust the nice value for a program as we launch it. This allows us to increase or decrease the priority given to the process by the kernel, relative to the other processes.
Let’s suppose a programmer has written a program called
ackermann . This calculates Ackerman functions. It is CPU and memory intensive. The programmer can launch the program with the following command:
We can use the
top command to view the running program.
We can see the details of the
ackermann program in
top. The nice value is the figure in the “NI column”. It set to zero as we’d expect.
Let’s restart it and this time make it less demanding. We’ll set a nice value of 15 for the
ackermann program as follows. Type nice, a space, -15, another space, and then the name of the program you wish to launch. In our example, our fictitious programmer is using
nice -15 ./ackermann
Take careful note, the “-15” is not negative fifteen. It is positive fifteen. The “-” is required to tell
nice we’re passing in a parameter. To indicate a negative number you must type two “-” characters.
If we now start
top again, we can see the change in the behavior of
With a nice value of 15,
ackermann is not consuming the most CPU time. GNOME and Rhythmbox are both using more. We’ve reined
ackermann in a bit.
Now let’s do the reverse and give
ackermann a negative nice value. Note the use of two “-” characters. To make an application more selfish and less nice, you must use
sudo. Anyone can make their application more nice, but only superusers can make one more selfish.
sudo nice --10 ./ackermann
Let’s run top and see what difference that has made.
ackermann has a nice value of -10. It is back on the top line and consuming more CPU time than before.
The renice Command
renice command lets us adjust the nice value of a running process. We don’t need to stop it and relaunch it with
nice. We can set a new value on-the-fly.
renice command takes the process ID, or PID, of the process as a command line parameter. We can either extract the process ID from the “PID” column in
top , or we can use
grep to find it for us, as follows. Obviously, you’ll type the name of your user instead of
dave and the name of the process you’re interested in instead of
ps -eu dave | grep ackermann
Now that we have the PID we can use this with
renice. We’re going to set
ackermann back to a nicer behavior with a nice value of five. To change the nice value for a running process you must use
sudo. Note that there is no “-” on the
5 parameter. You don’t need one for positive numbers and you only need one, not two, for negative numbers.
sudo renice -n 5 2339
We get confirmation that
renice has changed the nice value. It shows us the old value and the new value.
The kernel usually does a great job of handling priorities and handing out CPU time and systems resources. But if you have a long, CPU intensive task to run and you don’t care when it concludes, it’ll make your computer run a bit smoother if you set a higher nice value for that task. That will be nicer for everyone.