Control Process CPU Usage With nice and cpulimit [Linux]

No matter how fast your Linux machine is, there will always be some tasks that consume lots of CPU time. Everything from video encoding to running mathematical models can cause the CPU to run at 100%. This is fine if the machine isn’t doing anything else. But if it is your main desktop or it is used as a server, then running a task that max out your CPU will make the machine sluggish and hard to use. The beauty of a multitasking operating system is that the machine won’t freeze as it will always schedule CPU time for other tasks and things like the desktop will still work, but much slower. The scheduler has no way of knowing that you want the CPU intensive task to run at a lower priority or to limit its CPU time. However there are some tools which can be used to control process CPU usage for these resource hungry tasks.

To emulate a CPU intensive task, we can use a tool called matho-prime which generates a list of prime numbers. If you ask it to generate a big enough list it will max out the CPU while it is working. To install matho-prime on Ubuntu or on a Raspberry Pi use:

Let’s just start the task and see the CPU usage:

This tells matho-primes to generate a list of primes from zero to nine billion nine hundred ninety-nine million nine hundred ninety-nine thousand nine hundred ninety-nine! The output is redirected to the file p.txt. The ampersand at the end tells the shell to run the program in the background meaning you are returned to the command prompt straight after starting the task.

Now run the top command and notice the amount of CPU being used by the prime number generator.

matho-primes-max-cpu

Now let’s kill that task and look at how we can launch it at a lower priority. Exit top by pressing q, then type fg to bring matho-primes to the foreground and then press “Ctrl + C” to force the program to exit.

Now start the program again but at the beginning of the command add the word nice:

nice is a tool that tells Linux to schedule this task at a lower priority. The niceness ranges from -20 (most favorable scheduling) to 19 (least favorable). By default nice will schedule the task with a niceness of 10. By default normal tasks are started with a niceness of zero. This means that a task with a niceness of 10 will be seen as of a lower priority and will be given CPU resources less often.

We can now start a second one of these prime number generator to emulate other tasks that you want to do on your machine (like web browsing or writing a document). This one will have a normal niceness as using the desktop should be a greater priority than running the prime number generator.

Note that the output from this file is redirected to p2.txt. The output from top will look something like this:

matho-primes-nice

As you can see, the process with the higher niceness receives less CPU time. This means that if you launch a CPU intensive task with nice, it won’t make your machine feel sluggish or cause it to respond slowly.

cpulimit

There is a another command called “cpulimit” which rather than changing the scheduling priority, it temporarily pauses a process to ensure that it doesn’t exceed the desired CPU usage.

To install it type:

Now run the CPU intensive prime number generator again and note its PID number using this command:

The first column of the output is the PID. Now limit the CPU usage of that process like this:

Where 16299 is the PID of the matho-primes process. A look with top will show something like this:

matho-primes-cpulimit

Going further

There is a companion command to nice called renice which will change the niceness of an existing process. Trying use top or ps with grep to get the PID of a process and use renice +19 16299 to set the priority to the lowest possible (where 16299 is the PID of the process).

Feel free to add in the discussion in the comment below.

11 comments

  1. Your title says “Linux” but you only talk of Ubuntu. There are hundreds of other Linux distributions besides Ubuntu. How about providing information that can be used by SUSE, Mandriva or Fedora users. Or is MTE paid to push Ubuntu to the exclusion of other distros?!

    1. They probably use Ubuntu as an example because of it’s relative popularity (though I find it annoying as well). However, tools like nice/renice and top should be present with most distros, and they would work the same. As for the others mentioned here, they ought to be available in the repos of most distros.

      1. The tools may work the same but the commands to install them are not the same. The examples are very limited in their usefullness.

        nice/renice and cpulimit tools may be present in many distros but what about tools/programs that reside only in PPAs? The examples provided are totally useless for any distro other than *buntu.

        Bottom line is that MTE Linux articles are written as if Ubuntu was the only Linux distro. We, as users, did not switch from the monopoly of Microsoft to join the Canonical monopoly.

        1. We can’t, and won’t, possible cover all the distro out there. Ubuntu is used as the base for all our Linux articles simply because it is the platform we run our test on. For most of our Linux articles, we always try to keep it neutral and not distro-specific. Only when it is specifically for Ubuntu, then we will say “Ubuntu” instead of “Linux”.

        2. I joined the monopoly of MS and Canonical to not be bothered by people like you. Please feel free to go somewhere else for your tech tips.

      2. JahPickbey,

        Well said. Thanks.

        Gary

    2. Hi,

      Just to reiterate what Damien said below, we can’t possibly provided installation instruction for every distro. That would mean that large parts of the article would be taken up with installation instructions for every flavor of Linux. Also the article mentions Ubuntu and Raspberry Pi, not just Ubuntu, as I tested all my instructions on both a PC and on a Pi!

      Secondly, the information in the article is about controlling processes and everything I have written is true for other distros like Fedora or SUSE. If I did only provide Fedora examples then all the Ubuntu people would complain, we just can’t win!!!! :-)

      Thirdly, it isn’t difficult to convert the installation instructions from apt-get to yum. To install cpulimit just use “yum install cpulimit”. Granted the matho-primes tool is slightly different to install it use “yum install mathomatic-tools”. But that took me like 2 minutes of research using “yum search”. Not that hard really.

      Lastly, and I say this in humor, if you do know where we can get paid for just promoting Ubuntu over other distros them please do tell us as that could be a really money maker!!!

      :-) :-)

      Gary

  2. “For most of our Linux articles, we always try to keep it neutral and not distro-specific.”
    Maybe if you repeat that often enough to yourself, you’ll start to believe it. Because non of your readers believe it. Almost every Linux article IS distro-specific – Ubuntu specific. That’s too bad because the tools and solutions you write about could be usefull to the entire Linux community if we could install them on other distros.

    1. Dragonmouth,

      That is a little unfair. Everything in the article will work on other distros with the exception of the installation instruction. So 98% of the article applies equally to Fedora, SUSE, Ubuntu, Debian, Raspbian etc. And as I mentioned in my other reply it isn’t rocket science to convert “apt-get install…” to “yum install…”

      For future reference please tell me which is your favorite distro so that (if time permits) I can double check my instructions on it.

      Thanks, Gary.

  3. To solve the whining and complaining about distribution specific installation instructions, just say, “Use your favorite package installer for your Linux distribution to install _____.”. Then, you can link to an article explaining the basic package intallation process for a variety of distributions. Have a little entry for apt, one for yum, and so on. Finally explain that package names may differ depending on the repository and that the user should leave a comment on the article if they find mismatched package names.

    Also, people should be less whiny.

  4. Deal with it. If your using another distro then you should be savy enough to convert instruction from one distro to another. If you lack the skills to do research or figure it out on your own. Follow these instructions: 1. Turn off computer 2. Insert a Win 98 disc 3. Wait 4. Install drivers 5. Be Angry. Issue solved.

Comments are closed.